Here is my solution:
<?php
$frogs = explode(' ', readline());
$input = readline();
while ($input !== 'Print') {
$args = explode(' ', $input);
switch ($args[0]) {
//Join {name}":
//A frog comes on the riverside and you need to add it in the end of your collection. Frog names will never repeat.
case 'Join':
$name = $args[1];
if (!in_array($name, $frogs)) {
$frogs[] = $name;
}
break;
//Jump {name} {index}"
//A frog jumps out of the water and joins the other frogs. You need to add it in your collection on the given index, if the index exists.
case 'Jump':
$name = $args[1];
$index = $args[2];
if (key_exists($index, $frogs)) {
array_splice($frogs, $index, 0, $name);
}
break;
//Dive {index}":
//oThe frog on the given index has decided to jump into the water. You have to remove it from your collection, if the index exists.
case 'Dive':
$index = $args[1];
if (key_exists($index, $frogs)) {
array_splice($frogs, $index, 1);
}
//var_dump($frogs);
break;
//First/Last {count}":
//oPrint the first/last {count} frogs separated by a single space. If the count requested is more than the frogs- just print them to the end.
//"{frog} {frog} {frog}"
case 'First':
$count = $args[1];
if ($count > count($frogs)) {
echo implode(' ', $frogs) . PHP_EOL;
} else {
echo implode(' ', array_slice($frogs, 0, $count)) . PHP_EOL;
}
break;
case 'Last':
$count = $args[1];
if ($count > count($frogs)) {
echo implode(' ', $frogs) . PHP_EOL;
} else {
echo implode(' ', array_slice($frogs, count($frogs) - $count, $count)) . PHP_EOL;
}
break;
//Print Normal/Reversed"
//oPrint the names of the frogs in your collection in normal (in the order they have been added) or reversed order in the format described below, then stop the program:
//"Frogs: {frog1} {frog2}… {frogn}"
case 'Print':
$print = $args[1];
if ($print == 'Normal') {
echo 'Frogs: ' . implode(' ', $frogs);
}
if ($print == 'Reversed') {
echo 'Frogs: ' . implode(' ', array_reverse($frogs));
}
exit();
break;
}
$input = readline();
}