Advertisement
Guest User

Untitled

a guest
Oct 18th, 2018
212
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.76 KB | None | 0 0
  1. <?php
  2. $input = explode(" ", readline());
  3.  
  4. while(1) {
  5.     $command = explode(" ", readline());
  6.     if($command[0] == "end") {
  7.         break;
  8.     }
  9.     if($command[0] == "exchange") {
  10.         if($command[1] >= 0 && $command[1] < count($input) ) {
  11.         $input = exchange($input, $command[1]);
  12.  
  13.         } else {
  14.         echo "Invalid index" . PHP_EOL;
  15.         }
  16.     }
  17.     if($command[0] == "max") {
  18.         echo findMax($input, $command[1]) . PHP_EOL;
  19.     }
  20.     if($command[0] == "min") {
  21.         echo findMin($input, $command[1]) . PHP_EOL;
  22.     }
  23.     if($command[0] == "first") {
  24.         echo firstElements($input, $command) . PHP_EOL;
  25.     }
  26.     if($command[0] == "last") {
  27.         echo lastElements($input, $command) . PHP_EOL;
  28.     }
  29. }
  30.     echo "[" . implode(", ", $input) . "]";
  31.  
  32. function exchange($arr = array(), $index) {
  33.     $firstPart = array_slice($arr, 0, $index + 1);
  34.     $secondPart = array_slice($arr, $index + 1);
  35.     return array_merge($secondPart, $firstPart);
  36. }
  37. function lastElements($arr = array(), $command = array()) {
  38.     $count = $command[1];
  39.      if($count > count($arr)) {
  40.          return "Invalid count";
  41.      } else {
  42.          if($command[2] == "odd") {
  43.          $result = array_filter($arr, function ($input) {return $input & 1;});
  44.          } else {
  45.          $result = array_filter($arr, function ($input) {return !($input & 1);});
  46.          }
  47.          $result = array_slice($result, -$count);
  48.          return "[" . implode(", ", $result) . "]";
  49.      }
  50. }
  51. function firstElements($arr = array(), $command = array()) {
  52.     $count = $command[1];
  53.      if($count > count($arr)) {
  54.          return "Invalid count";
  55.      } else {
  56.          if($command[2] == "odd") {
  57.          $result = array_filter($arr, function ($input) {return $input & 1;});
  58.          } else {
  59.          $result = array_filter($arr, function ($input) {return !($input & 1);});
  60.          }
  61.          $result = array_slice($result, 0, $count);
  62.          return "[" . implode(", ", $result) . "]";
  63.      }
  64. }
  65. function findMin($arr = array(), $type) {
  66.     $minOdd = PHP_INT_MAX;
  67.     $minEven = PHP_INT_MAX;
  68.      for($i = 0; $i < count($arr); $i++) {
  69.          if($arr[$i] % 2 != 0 && $arr[$i] <= $minOdd) {
  70.              $minOdd = $arr[$i];
  71.          } else if($arr[$i] % 2 == 0 && $arr[$i] <= $minEven) {
  72.              $minEven = $arr[$i];
  73.          }
  74.      }
  75.       if($type == "odd") {
  76.           $arr_keys = array_keys($arr, $minOdd);
  77.           $minOddIndex = array_pop($arr_keys);
  78.           if($minOddIndex !== false) {
  79.               return $minOddIndex;
  80.           } else {
  81.               return "No matches";
  82.           }
  83.       } else {
  84.           $arr_keys = array_keys($arr, $minEven);
  85.           $minEvenIndex = array_pop($arr_keys);
  86.           if($minEvenIndex) {
  87.               return $minEvenIndex;
  88.           } else {
  89.               return "No matches";
  90.           }
  91.       }
  92.      
  93. }
  94. function findMax($arr = array(), $type) {
  95.     $maxOdd = PHP_INT_MIN;
  96.     $maxEven = PHP_INT_MIN;
  97.      for($i = 0; $i < count($arr); $i++) {
  98.          if($arr[$i] % 2 != 0 && $arr[$i] > $maxOdd) {
  99.              $maxOdd = $arr[$i];
  100.          } else if($arr[$i] % 2 == 0 && $arr[$i] > $maxEven) {
  101.              $maxEven = $arr[$i];
  102.          }
  103.      }
  104.       if($type == "odd") {
  105.           $arr_keys = array_keys($arr, $maxOdd);
  106.           $maxOddIndex = array_pop($arr_keys);
  107.           if($maxOddIndex !== false) {
  108.               return $maxOddIndex;
  109.           } else {
  110.               return "No matches";
  111.           }
  112.       } else {
  113.           $arr_keys = array_keys($arr, $maxEven);
  114.           $maxEvenIndex = array_pop($arr_keys);
  115.           if($maxEvenIndex) {
  116.               return $maxEvenIndex;
  117.           } else {
  118.               return "No matches";
  119.           }
  120.       }
  121.      
  122. }
  123. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement