Advertisement
ShadowEmbrace

Change Array

Nov 4th, 2018
153
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.91 KB | None | 0 0
  1. <?php
  2.  
  3. $array = array_map('intval', explode(' ', readline()));
  4.  
  5. $input = readline();
  6.  
  7. while (true) {
  8.     $commands = explode(' ', $input);
  9.     if ($commands[0] === 'Delete') {
  10.         for ($i = 0; $i < count($array); $i++) {
  11.             if (intval($commands[1]) === $array[$i]) {
  12.                 array_splice($array, $i, 1);
  13.                 $i--;
  14.             }
  15.         }
  16.     } elseif ($commands[0] === 'Insert') {
  17.         array_splice($array, intval($commands[2]), 0, intval($commands[1]));
  18.  
  19.     } elseif ($commands[0] === 'Odd') {
  20.         foreach ($array as $item) {
  21.             if ($item % 2 === 1) {
  22.                 echo $item . " ";
  23.             }
  24.         }
  25.         break;
  26.     } elseif ($commands[0] === 'Even') {
  27.         foreach ($array as $item) {
  28.             if ($item % 2 === 0) {
  29.                 echo $item . " ";
  30.             }
  31.         }
  32.         break;
  33.     }
  34.     $input = readline();
  35. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement