Advertisement
MartinGeorgiev

Untitled

Oct 15th, 2018
147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.36 KB | None | 0 0
  1. <?php
  2. /**
  3.  * Created by PhpStorm.
  4.  * User: Marto
  5.  * Date: 15/10/18
  6.  * Time: 13:53
  7.  */
  8. $nums = array_map('intval', explode(' ', readline()));
  9. $tokens = array_map('strval', explode(' ', readline()));
  10. while ($tokens[0] != "end") {
  11.     $command = $tokens[0];
  12.     // $command2 = strval(implode("", $tokens[1]));
  13.     if ($command == "end") {
  14.         break;
  15.     }
  16.     switch ($command) {
  17.         case"add":
  18.             $command2 = $tokens[1];
  19.             $command3 = $tokens[2];
  20.             $index = intval($command2);
  21.             $element = intval($command3);
  22.             insert($nums, $index, $element);
  23.             break;
  24.         case"exchange":
  25.  
  26.             $positions = intval($tokens[1]);
  27.             exchange($nums, $positions);
  28.             break;
  29.         case"max":
  30.             $type = $tokens[1];
  31.             printMax($nums, $type);
  32.             break;
  33.         case"dump":
  34.             print_r($command);
  35.             print_r($nums);
  36.             break;
  37.         case "min":
  38.             $type = $tokens[1];
  39.             printMin($nums, $type);
  40.             break;
  41.         default:
  42.  
  43.             break;
  44.     }
  45.     unset ($tokens);
  46.     $tokens = array_map('strval', explode(' ', readline()));
  47.     $command = $tokens[0];
  48. }
  49. for ($i = 0; $i < count($nums); $i++) {
  50.     $current = intval($nums[$i]);
  51.     echo "$current ";
  52. }
  53. function insert(&$array, $position, $insert)
  54. {
  55.     array_splice($array, $position, 0, $insert);
  56. }
  57.  
  58. function exchange(&$nums, $positions)
  59. {
  60.  
  61.     $realPositions = intval($positions % count($nums));
  62.     for ($i = 0; $i < $realPositions; $i++) {
  63.         $temp = $nums[0];
  64.         array_splice($nums, 0, 1);
  65.         array_push($nums, intval($temp));
  66.     }
  67.  
  68. }
  69.  
  70. function printMin(&$nums, $type)
  71. {
  72.     $min = -10000;
  73.     $index = 0;
  74.     $flag = false;
  75.     if ($type === "odd") {
  76.         for ($i = 0; $i < sizeof($nums); $i++) {
  77.             if ($nums[$i] % 2 == 1) {
  78.                 if ($nums[$i] < $min) {
  79.                     $min = $nums[$i];
  80.                     $index = $i;
  81.                     $flag = true;
  82.                 }
  83.             }
  84.         }
  85.  
  86.  
  87.     } else {
  88.         if ($type === "even") {
  89.             for ($i = 0; $i < sizeof($nums); $i++) {
  90.                 if ($nums[$i] % 2 == 0) {
  91.                     if ($nums[$i] < $min) {
  92.                         $min = $nums[$i];
  93.                         $index = $i;
  94.                         $flag = true;
  95.                     }
  96.                 }
  97.             }
  98.         }
  99.     }
  100.     if ($flag) {
  101.         echo $index . PHP_EOL;
  102.     }else {
  103.         echo "No matches";
  104.     }
  105. }
  106.  
  107. function printMax(&$nums, $type)
  108. {
  109.     $max = 0;
  110.     $index = 0;
  111.     $flag = false;
  112.     if ($type === "odd") {
  113.         for ($i = 0; $i < sizeof($nums); $i++) {
  114.             if ($nums[$i] % 2 == 1) {
  115.                 if ($nums[$i] > $max) {
  116.                     $max = $nums[$i];
  117.                     $index = $i;
  118.                     $flag = true;
  119.                 }
  120.             }
  121.         }
  122.  
  123.     } else {
  124.         for ($i = 0; $i < sizeof($nums); $i++) {
  125.             if ($nums[$i] % 2 == 0) {
  126.                 if ($nums[$i] > $max) {
  127.                     $max = $nums[$i];
  128.                     $index = $i;
  129.                     $flag = true;
  130.                 }
  131.             }
  132.         }
  133.  
  134.     }
  135.     if ($flag) {
  136.         echo $index . PHP_EOL;
  137.     }else {
  138.         echo "No matches";
  139.     }
  140. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement