Advertisement
Guest User

Untitled

a guest
Oct 23rd, 2018
285
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.10 KB | None | 0 0
  1. <?php
  2. $arr = array_map('floatval', explode(" ", readline()));
  3.  while(1) {
  4.      $command = readline();
  5.      if($command == "end") {
  6.          break;
  7.      }
  8.      $comm = explode(" ", $command);
  9.      if($command == "Get sum") {
  10.          echo array_sum($arr) . PHP_EOL;
  11.      }
  12.      switch($comm[0]) {
  13.          case 'Contains':
  14.              if(in_array($comm[1], $arr)) {
  15.                  echo "Yes" . PHP_EOL;
  16.              } else {
  17.                  echo "No such number" . PHP_EOL;
  18.              }
  19.          break;
  20.          case 'Print':
  21.              $num = $comm[1];
  22.              if($num == "even") {
  23.                  printEven($arr);
  24.              } else if($num == "odd") {
  25.                  printOdd($arr);
  26.              }
  27.          break;
  28.          case "Filter":
  29.              $condition = $comm[1];
  30.              $num = $comm[2];
  31.              Filter($arr, $condition, $num);
  32.          break;
  33.      }
  34.  }
  35. echo implode(" ", $arr);
  36.  
  37. function Filter($arr = array(), $condition, $num) {
  38.     $length = count($arr);
  39.     $temp = array();
  40.      for($i = 0; $i < $length; $i++) {
  41.          if($condition == "<") {
  42.              if($arr[$i] < $num) {
  43.                  $temp[] = $arr[$i];
  44.              }
  45.          } else if($condition == ">") {
  46.              if($arr[$i] > $num) {
  47.                  $temp[] = $arr[$i];
  48.              }
  49.          } else if($condition == ">=") {
  50.              if($arr[$i] >= $num) {
  51.                  $temp[] = $arr[$i];
  52.              }
  53.          }  else if($condition == "<=") {
  54.              if($arr[$i] <= $num) {
  55.                  $temp[] = $arr[$i];
  56.              }
  57.          }
  58.      }
  59.      echo implode(" ", $temp) . PHP_EOL;
  60. }
  61. function printOdd($arr = array()) {
  62.     $temp = array();
  63.     for($i = 0; $i < count($arr); $i++) {
  64.         if($arr[$i] % 2 != 0) {
  65.             $temp[] = $arr[$i];
  66.         }
  67.     }
  68.     echo implode(" ", $temp) . PHP_EOL;
  69. }
  70.  
  71. function printEven($arr = array()) {
  72.     $temp = array();
  73.     for($i = 0; $i < count($arr); $i++) {
  74.         if($arr[$i] % 2 == 0) {
  75.             $temp[] = $arr[$i];
  76.         }
  77.     }
  78.     echo implode(" ", $temp) . PHP_EOL;
  79. }
  80.  
  81. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement