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 = array_map('strval', explode(' ', readline()));
- while ($tokens[0] != "end") {
- $command = $tokens[0];
- // $command2 = strval(implode("", $tokens[1]));
- if ($command == "end") {
- break;
- }
- switch ($command) {
- case"add":
- $command2 = $tokens[1];
- $command3 = $tokens[2];
- $index = intval($command2);
- $element = intval($command3);
- insert($nums, $index, $element);
- break;
- case"exchange":
- $positions = intval($tokens[1]);
- exchange($nums, $positions);
- break;
- case"max":
- $type = $tokens[1];
- printMax($nums, $type);
- break;
- case"dump":
- print_r($command);
- print_r($nums);
- break;
- case "min":
- $type = $tokens[1];
- printMin($nums, $type);
- break;
- default:
- break;
- }
- unset ($tokens);
- $tokens = array_map('strval', explode(' ', readline()));
- $command = $tokens[0];
- }
- 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];
- array_splice($nums, 0, 1);
- array_push($nums, intval($temp));
- }
- }
- function printMin(&$nums, $type)
- {
- $min = -10000;
- $index = 0;
- $flag = false;
- if ($type === "odd") {
- for ($i = 0; $i < sizeof($nums); $i++) {
- if ($nums[$i] % 2 == 1) {
- if ($nums[$i] < $min) {
- $min = $nums[$i];
- $index = $i;
- $flag = true;
- }
- }
- }
- } else {
- if ($type === "even") {
- for ($i = 0; $i < sizeof($nums); $i++) {
- if ($nums[$i] % 2 == 0) {
- if ($nums[$i] < $min) {
- $min = $nums[$i];
- $index = $i;
- $flag = true;
- }
- }
- }
- }
- }
- if ($flag) {
- echo $index . PHP_EOL;
- }else {
- echo "No matches";
- }
- }
- function printMax(&$nums, $type)
- {
- $max = 0;
- $index = 0;
- $flag = false;
- if ($type === "odd") {
- for ($i = 0; $i < sizeof($nums); $i++) {
- if ($nums[$i] % 2 == 1) {
- if ($nums[$i] > $max) {
- $max = $nums[$i];
- $index = $i;
- $flag = true;
- }
- }
- }
- } else {
- for ($i = 0; $i < sizeof($nums); $i++) {
- if ($nums[$i] % 2 == 0) {
- if ($nums[$i] > $max) {
- $max = $nums[$i];
- $index = $i;
- $flag = true;
- }
- }
- }
- }
- if ($flag) {
- echo $index . PHP_EOL;
- }else {
- echo "No matches";
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement