[Löst] The Final Quest - PHP Exam Task

  

3
Ämnesstart

After walking through fire, the group has reached the final step of the quest. They have received a list with instructions that will help them resolve the last riddle that will lead them to the truth about the Hunting Games.

Create a program that follows given instructions:

You will receive a collection of words on a single line, split by a single space. They are not what they are supposed to be, so you have to follow the instructions in order to find the real message. You will be receiving commands. Here are the possible ones:

  • Delete {index} – removes the word after the given index if it is valid.
  • Swap {word1} {word2} – find the given words in the collections if they exist and swap their places.
  • Put {word} {index} – add a word at the previous place {index} before the given one, if it is valid. Note: putting at the last index simply appends the word to the end of the list.
  • Sort – you must sort the words in descending order.
  • Replace {word1} {word2} – find the second word {word2} in the collection (if it exists) and replace it with the first word – {word1}.

Follow them until you receive the "Stop" command. After you have successfully followed the instructions, you must print the words on a single line, split by a space.

Input / Constraints:

  • On the 1st line, you are going to receive the collection of words, split by a single space – strings
  • On the next lines, you are going to receive commands, until you receive the "Stop" command

Output:

  • Print the words you have gathered on a single line, split by a single space

Examples:

the final quest php exam task

1 Answer
2

Here is my solution:

<?php
 
$arr = explode(" ", readline());
$input = readline();
 
while ($input != "Stop") {
    $args = explode(" ", $input);
    $command = $args[0];
 
    switch ($command) {
        case 'Delete':
            if (($args[1] + 1) < count($arr) && ($args[1] + 1) >= 0) {
                array_splice($arr, $args[1] + 1, 1);//removes 1 next element in the array with nothing
            }
            break;
        case 'Swap':
            if (in_array($args[1], $arr) && in_array($args[2], $arr)) {
                $index1 = array_search($args[1], $arr);
                $index2 = array_search($args[2], $arr);
                array_splice($arr, $index1, 1, $args[2]);//replace a word with another
                array_splice($arr, $index2, 1, $args[1]);//replace another word with a word
            }
            break;
        case 'Put':
            if (($args[2] - 1) <= count($arr) && ($args[2] - 1) >= 0) {
                array_splice($arr, $args[2] - 1, 0, $args[1]);//add the new element
            }
            break;
        case 'Sort':
            rsort($arr);//sorting in descending order
            break;
        case 'Replace':
            if (in_array($args[2], $arr)) {
                array_splice($arr, array_search($args[2], $arr), 1, $args[1]);//replace the 2nd element with the 1st
            }
            break;
    }
 
    $input = readline();
}
 
echo implode(" ", $arr);
Dela: