Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- $input = explode(" ", readline());
- while(1) {
- $command = explode(" ", readline());
- if($command[0] == "end") {
- break;
- }
- if($command[0] == "exchange") {
- if($command[1] >= 0 && $command[1] < count($input) ) {
- $input = exchange($input, $command[1]);
- } else {
- echo "Invalid index" . PHP_EOL;
- }
- }
- if($command[0] == "max") {
- echo findMax($input, $command[1]) . PHP_EOL;
- }
- if($command[0] == "min") {
- echo findMin($input, $command[1]) . PHP_EOL;
- }
- if($command[0] == "first") {
- echo firstElements($input, $command) . PHP_EOL;
- }
- if($command[0] == "last") {
- echo lastElements($input, $command) . PHP_EOL;
- }
- }
- echo "[" . implode(", ", $input) . "]";
- function exchange($arr = array(), $index) {
- $firstPart = array_slice($arr, 0, $index + 1);
- $secondPart = array_slice($arr, $index + 1);
- return array_merge($secondPart, $firstPart);
- }
- function lastElements($arr = array(), $command = array()) {
- $count = $command[1];
- if($count > count($arr)) {
- return "Invalid count";
- } else {
- if($command[2] == "odd") {
- $result = array_filter($arr, function ($input) {return $input & 1;});
- } else {
- $result = array_filter($arr, function ($input) {return !($input & 1);});
- }
- $result = array_slice($result, -$count);
- return "[" . implode(", ", $result) . "]";
- }
- }
- function firstElements($arr = array(), $command = array()) {
- $count = $command[1];
- if($count > count($arr)) {
- return "Invalid count";
- } else {
- if($command[2] == "odd") {
- $result = array_filter($arr, function ($input) {return $input & 1;});
- } else {
- $result = array_filter($arr, function ($input) {return !($input & 1);});
- }
- $result = array_slice($result, 0, $count);
- return "[" . implode(", ", $result) . "]";
- }
- }
- function findMin($arr = array(), $type) {
- $minOdd = PHP_INT_MAX;
- $minEven = PHP_INT_MAX;
- for($i = 0; $i < count($arr); $i++) {
- if($arr[$i] % 2 != 0 && $arr[$i] <= $minOdd) {
- $minOdd = $arr[$i];
- } else if($arr[$i] % 2 == 0 && $arr[$i] <= $minEven) {
- $minEven = $arr[$i];
- }
- }
- if($type == "odd") {
- $arr_keys = array_keys($arr, $minOdd);
- $minOddIndex = array_pop($arr_keys);
- if($minOddIndex !== false) {
- return $minOddIndex;
- } else {
- return "No matches";
- }
- } else {
- $arr_keys = array_keys($arr, $minEven);
- $minEvenIndex = array_pop($arr_keys);
- if($minEvenIndex) {
- return $minEvenIndex;
- } else {
- return "No matches";
- }
- }
- }
- function findMax($arr = array(), $type) {
- $maxOdd = PHP_INT_MIN;
- $maxEven = PHP_INT_MIN;
- for($i = 0; $i < count($arr); $i++) {
- if($arr[$i] % 2 != 0 && $arr[$i] > $maxOdd) {
- $maxOdd = $arr[$i];
- } else if($arr[$i] % 2 == 0 && $arr[$i] > $maxEven) {
- $maxEven = $arr[$i];
- }
- }
- if($type == "odd") {
- $arr_keys = array_keys($arr, $maxOdd);
- $maxOddIndex = array_pop($arr_keys);
- if($maxOddIndex !== false) {
- return $maxOddIndex;
- } else {
- return "No matches";
- }
- } else {
- $arr_keys = array_keys($arr, $maxEven);
- $maxEvenIndex = array_pop($arr_keys);
- if($maxEvenIndex) {
- return $maxEvenIndex;
- } else {
- return "No matches";
- }
- }
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement