Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /**
- * Created by PhpStorm.
- * User: Marto
- * Date: 15/10/18
- * Time: 13:53
- */
- $nums = array_map('intval', explode(' ', readline()));
- $tokens[] = explode(' ', readline());
- while ($tokens[0] != "end") {
- $command = strval(implode("", $tokens[0]));
- if ($command == "end") {
- break;
- }
- switch ($tokens[0]) {
- case"add":
- $index = intval($tokens[1]);
- $element = intval($tokens[2]);
- insert($nums, $index, $element);
- break;
- case"exchange":
- $positions = intval($tokens[1]);
- echo $positions;
- exchange($nums, $positions);
- break;
- case"max":
- $type = strval(implode("", $tokens[1]));
- echo $command.PHP_EOL;
- echo $type;
- //$result= printMax($nums, $type);
- // echo $result;
- break;
- case"dump":
- print_r($tokens[0]);
- print_r($nums);
- break;
- default:
- break;
- }
- $tokens[] = explode(' ', readline());
- }
- for ($i = 0; $i < count($nums); $i++) {
- $current = intval($nums[$i]);
- echo "$current ";
- }
- function insert(&$array, $position, $insert)
- {
- array_splice($array, $position, 0, $insert);
- }
- function exchange(&$nums, $positions)
- {
- $realPositions = intval($positions % count($nums));
- for ($i = 0; $i < $realPositions; $i++) {
- $temp = $nums[0];
- unset($nums[0]);
- array_push($nums, $temp);
- echo "$nums[$i] ";
- }
- }
- function printMax(&$nums, $type)
- {
- $max = 0;
- $index=0;
- if ($type === "odd") {
- for ($i = 0; $i < sizeof($nums); $i++) {
- if ($nums[$i] % 2 == 1) {
- if ($nums[$i] > $max) {
- $max = $nums[$i];
- $index=$i;
- }
- }
- }
- } else {
- if ($type === "even") {
- for ($i = 0; $i < sizeof($nums); $i++) {
- if ($nums[$i] % 2 == 0) {
- if ($nums[$i] > $max) {
- $max = $nums[$i];
- $index=$i;
- }
- }
- }
- }
- }
- return $index;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement