Advertisement
MartinGeorgiev

11. Array Manipulator

Oct 22nd, 2018
321
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 5.01 KB | None | 0 0
  1. <?php
  2. $array = array_map("intval", explode(" ", readline()));
  3. $input = readline();
  4.  
  5. while (true) {
  6.     $token = explode(" ", $input);
  7.     $command = $token[0];
  8.     if ($command === "end") {
  9.         break;
  10.     }
  11.     switch ($command) {
  12.         case"exchange":
  13.             $index = intval($token[1]);
  14.             $length = count($array);
  15.             if ($index >= $length || $index < 0) {
  16.                 echo "Invalid index" . PHP_EOL;
  17.             } else {
  18.                 $array = exchange($array, $index);
  19.             }
  20.             break;
  21.         case "max":
  22.             $type = $token[1];
  23.             printMax($array, $type);
  24.             break;
  25.         case "min":
  26.             $type = $token[1];
  27.             printMin($array, $type);
  28.             break;
  29.         case "first":
  30.             $count = intval($token[1]);
  31.             $type = $token[2];
  32.             if ($count > count($array)) {
  33.                 echo "Invalid count" . PHP_EOL;
  34.             } else {
  35.                 printFirstType($array, $count, $type);
  36.             }
  37.             break;
  38.         case "last":
  39.             $count = intval($token[1]);
  40.             $type = $token[2];
  41.             if ($count > count($array)) {
  42.                 echo "Invalid count" . PHP_EOL;
  43.             } else {
  44.                 printLastType($array, $count, $type);
  45.             }
  46.             break;
  47.  
  48.     }
  49.  
  50.  
  51.     $input = readline();
  52. }
  53. echo "[" . implode(", ", $array) . "]";
  54.  
  55. function exchange($arr, int $index)
  56. {
  57.     $newArr = [];
  58.     $length = count($arr);
  59.     for ($i = $index + 1; $i < $length; $i++) {
  60.         $newArr[] = $arr[$i];
  61.     }
  62.     for ($i = 0; $i <= $index; $i++) {
  63.         $newArr[] = $arr[$i];
  64.     }
  65.     return $newArr;
  66. }
  67.  
  68. function printMax($array, string $type)
  69. {
  70.     $length = count($array);
  71.     $index = -1;
  72.     $max = PHP_INT_MIN;
  73.     if ($type == "even") {
  74.         for ($i = 0; $i < $length; $i++) {
  75.             if ($array[$i] % 2 == 0) {
  76.                 if ($array[$i] >= $max) {
  77.                     $index = $i;
  78.                     $max = $array[$i];
  79.                 }
  80.             }
  81.         }
  82.     }
  83.     if ($type == "odd") {
  84.         for ($i = 0; $i < $length; $i++) {
  85.             if ($array[$i] % 2 == 1) {
  86.                 if ($array[$i] >= $max) {
  87.                     $index = $i;
  88.                     $max = $array[$i];
  89.                 }
  90.             }
  91.         }
  92.     }
  93.     if ($index === -1) {
  94.         echo "No matches" . PHP_EOL;
  95.     } else {
  96.         echo $index . PHP_EOL;
  97.     }
  98.  
  99. }
  100.  
  101. function printMin($array, string $type)
  102. {
  103.     $length = count($array);
  104.     $index = -1;
  105.     $min = PHP_INT_MAX;
  106.     if ($type == "even") {
  107.         for ($i = 0; $i < $length; $i++) {
  108.             if ($array[$i] % 2 == 0) {
  109.                 if ($array[$i] <= $min) {
  110.                     $index = $i;
  111.                     $min = $array[$i];
  112.                 }
  113.             }
  114.         }
  115.     }
  116.     if ($type == "odd") {
  117.         for ($i = 0; $i < $length; $i++) {
  118.             if ($array[$i] % 2 == 1) {
  119.                 if ($array[$i] <= $min) {
  120.                     $index = $i;
  121.                     $min = $array[$i];
  122.                 }
  123.             }
  124.         }
  125.     }
  126.     if ($index === -1) {
  127.         echo "No matches" . PHP_EOL;
  128.     } else {
  129.         echo $index . PHP_EOL;
  130.     }
  131. }
  132.  
  133. function printFirstType($array, int $count, $type)
  134. {
  135.  
  136.     $newArr = [];
  137.     $size = count($array);
  138.     $success = 0;
  139.     if ($type == "even") {
  140.         for ($i = 0; $i < $size; $i++) {
  141.             if ($array[$i] % 2 == 0) {
  142.                 if ($success === $count) {
  143.                     break;
  144.                 }
  145.                 $newArr[] = $array[$i];
  146.                 $success++;
  147.             }
  148.         }
  149.     }
  150.  
  151.     if ($type == "odd") {
  152.         for ($i = 0; $i < $size; $i++) {
  153.             if ($array[$i] % 2 == 1) {
  154.                 if ($success === $count) {
  155.                     break;
  156.                 }
  157.                 $newArr[] = $array[$i];
  158.                 $success++;
  159.             }
  160.         }
  161.     }
  162.     if (count($newArr) === 0) {
  163.         echo "[]" . PHP_EOL;
  164.     } else {
  165.         echo "[" . implode(", ", $newArr) . "]" . PHP_EOL;
  166.     }
  167.  
  168. }
  169.  
  170.  
  171.  
  172. function printLastType($array, int $count, $type)
  173. {
  174.  
  175.     $newArr = [];
  176.     $size = count($array);
  177.     $success = 0;
  178.     if ($type == "even") {
  179.         for ($i = $size-1; $i >= 0; $i--) {
  180.             if ($array[$i] % 2 == 0) {
  181.                 if ($success === $count) {
  182.                     break;
  183.                 }
  184.                 $newArr[] = $array[$i];
  185.                 $success++;
  186.             }
  187.         }
  188.     }
  189.  
  190.     if ($type == "odd") {
  191.         for ($i = $size-1; $i >= 0; $i--) {
  192.             if ($array[$i] % 2 == 1) {
  193.                 if ($success === $count) {
  194.                     break;
  195.                 }
  196.                 $newArr[] = $array[$i];
  197.                 $success++;
  198.             }
  199.         }
  200.     }
  201.     if (count($newArr) === 0) {
  202.         echo "[]" . PHP_EOL;
  203.     } else {
  204.         echo "[" . implode(", ", array_reverse($newArr)) . "]" . PHP_EOL;
  205.     }
  206. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement