Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- $numbers = array_map('intval', explode(' ', readline()));
- do{
- $input = readline();
- $commands = array_map('trim',explode(' ', $input));
- if (strtolower($commands[0]) === 'add'){
- $numbers[] = $commands[1];
- }elseif (strtolower($commands[0]) === 'insert'){
- if (array_key_exists($commands[2], $numbers)){
- array_splice($numbers, intval($commands[2]), 0, intval($commands[1]));
- }else{
- echo 'Invalid index' . PHP_EOL;
- }
- }elseif (strtolower($commands[0]) === 'remove'){
- if (array_key_exists($commands[1], $numbers)){
- array_splice($numbers, intval($commands[1]), 1);
- }else{
- echo 'Invalid index' . PHP_EOL;
- }
- }elseif ($commands[0] === 'Shift' && $commands[1] === 'left'){
- for ($i = 0; $i < intval($commands[2]); $i++){
- $shiftNum = $numbers[0];
- array_shift($numbers);
- $numbers[] = $shiftNum;
- }
- }elseif ($commands[0] === 'Shift' && $commands[1] === 'right'){
- for ($i = 0; $i < intval($commands[2]); $i++){
- array_unshift($numbers, $numbers[count($numbers)-1]);
- array_pop($numbers);
- }
- }
- }while ($input !== "End");
- echo implode(' ', $numbers);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement