Advertisement
MartinGeorgiev

Untitled

Oct 15th, 2018
158
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.25 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[] = explode(' ', readline());
  10. while ($tokens[0] != "end") {
  11.  
  12.     $command = strval(implode("", $tokens[0]));
  13.     if ($command == "end") {
  14.         break;
  15.     }
  16.     switch ($tokens[0]) {
  17.         case"add":
  18.             $index = intval($tokens[1]);
  19.             $element = intval($tokens[2]);
  20.             insert($nums, $index, $element);
  21.             break;
  22.         case"exchange":
  23.             $positions = intval($tokens[1]);
  24.             echo $positions;
  25.             exchange($nums, $positions);
  26.             break;
  27.         case"max":
  28.             $type = strval(implode("", $tokens[1]));
  29.             echo $command.PHP_EOL;
  30.             echo $type;
  31.             //$result= printMax($nums, $type);
  32.            // echo $result;
  33.             break;
  34.         case"dump":
  35.             print_r($tokens[0]);
  36.             print_r($nums);
  37.             break;
  38.         default:
  39.  
  40.             break;
  41.     }
  42.     $tokens[] = explode(' ', readline());
  43. }
  44. for ($i = 0; $i < count($nums); $i++) {
  45.     $current = intval($nums[$i]);
  46.     echo "$current ";
  47. }
  48. function insert(&$array, $position, $insert)
  49. {
  50.     array_splice($array, $position, 0, $insert);
  51. }
  52.  
  53. function exchange(&$nums, $positions)
  54. {
  55.     $realPositions = intval($positions % count($nums));
  56.     for ($i = 0; $i < $realPositions; $i++) {
  57.         $temp = $nums[0];
  58.         unset($nums[0]);
  59.         array_push($nums, $temp);
  60.         echo "$nums[$i] ";
  61.     }
  62.  
  63. }
  64.  
  65. function printMax(&$nums, $type)
  66. {
  67.     $max = 0;
  68.     $index=0;
  69.     if ($type === "odd") {
  70.         for ($i = 0; $i < sizeof($nums); $i++) {
  71.             if ($nums[$i] % 2 == 1) {
  72.                 if ($nums[$i] > $max) {
  73.                     $max = $nums[$i];
  74.                     $index=$i;
  75.                 }
  76.             }
  77.         }
  78.  
  79.  
  80.     } else {
  81.         if ($type === "even") {
  82.             for ($i = 0; $i < sizeof($nums); $i++) {
  83.                 if ($nums[$i] % 2 == 0) {
  84.                     if ($nums[$i] > $max) {
  85.                         $max = $nums[$i];
  86.                         $index=$i;
  87.                     }
  88.                 }
  89.             }
  90.         }
  91.     }
  92.     return $index;
  93. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement