Advertisement
Guest User

Untitled

a guest
Aug 24th, 2019
216
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.44 KB | None | 0 0
  1. <?php
  2. $word = explode(" ", readline());
  3. $input = readline();
  4. while ($input != "Stop") {
  5. $comand = explode(" ", $input);
  6. switch ($comand[0]) {
  7. case"Delete":
  8. $index = $comand[1] + 1;
  9. if ($index >= 0 && $index <= count($word)) {
  10. array_splice($word, $index, 1);
  11. }
  12. break;
  13. case"Swap":
  14. $word1 = $comand[1];
  15. $word2 = $comand[2];
  16. if (in_array($word1, $word) && in_array($word2, $word)) {
  17. $temp = $word1;
  18. $index1 = array_search($word1, $word);
  19. $index2 = array_search($word2, $word);
  20. array_splice($word, $index1, 1, $word2);
  21. array_splice($word, $index2, 1, $temp);
  22.  
  23. }
  24. break;
  25. case"Put":
  26. $element = $comand[1];
  27. $index = $comand[2];
  28. if ($index >= 0 && $index <= count($word)+1) {
  29. array_splice($word, $index - 1, 0, $element);
  30. }
  31. break;
  32. case"Sort":
  33. rsort($word);
  34. break;
  35. case"Replace":
  36. $word1 = $comand[1];
  37. $word2 = $comand[2];
  38. if (in_array($word2, $word)) {
  39. $index = array_search($word2, $word);
  40. array_splice($word, $index, 1, $word1);
  41. }
  42. break;
  43. }
  44. $input = readline();
  45. }
  46. echo implode(" ", $word);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement