Advertisement
plezzz

03. The Final Quest

Mar 10th, 2019
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.51 KB | None | 0 0
  1. <?php
  2. /**
  3.  * Created by PhpStorm.
  4.  * User: Niki
  5.  * Date: 10/03/2019
  6.  * Time: 14:16
  7.  */
  8.  
  9. $words = explode(" ", readline());
  10.  
  11. while (true) {
  12.     $input = readline();
  13.     if ($input == "Stop")
  14.         break;
  15.     list($command, $tokenOne, $tokenTwo) = array_pad(explode(" ", $input), 3, null);
  16.  
  17.     switch ($command) {
  18.         case "Delete":
  19.             $index = $tokenOne + 1;
  20.             if ($index < count($words) && $index >= 0)
  21.                 array_splice($words, $index, 1);
  22.             break;
  23.         case "Swap":
  24.             if (in_array($tokenOne, $words)) {
  25.                 if (in_array($tokenTwo, $words)) {
  26.                     $keyOne = array_search($tokenOne, $words);
  27.                     $keyTwo = array_search($tokenTwo, $words);
  28.                     $temp = $words[$keyOne];
  29.                     array_splice($words, $keyOne, 1, $words[$keyTwo]);
  30.                     array_splice($words, $keyTwo, 1, $temp);
  31.                 }
  32.             }
  33.             break;
  34.         case "Put":
  35.             $index = $tokenTwo - 1;
  36.             if ($index < count($words)+1 && $index >= 0){
  37.                 array_splice($words, $index, 0, $tokenOne);
  38.             }
  39.             break;
  40.         case "Sort":
  41.             rsort($words);
  42.             break;
  43.         case "Replace":
  44.             if (in_array($tokenTwo, $words)) {
  45.                 $key = array_search($tokenTwo, $words);
  46.                 array_splice($words, $key, 1, $tokenOne);
  47.             }
  48.             break;
  49.     }
  50. }
  51. echo implode(" ",$words);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement