11. Array Manipulator | Tech Module 4.0 | PHP
Много интересна задача - получавам 90/100, дали може някой който я е решавал на PHP да сподели? Има някаква ловка в задачата и не мога да я намеря вече няколко часа. Решил съм я по много заплетен начин но все пак ще оставя кода отдолу.
<?php
$arr = explode(" ", readline());
for($i = 0; $i < count($arr); $i++) {
$arr[$i] = intval($arr[$i]);
}
//var_dump($arr);
while(true) {
$commands = explode(" ", readline());
if ($commands[0] == "end") { break; }
// BEGIN exchange {index}
if ($commands[0] == "exchange") {
$command = $commands[0];
$index = $commands[1];
if ($index >= count($arr)) {
echo "Invalid index" . PHP_EOL;
} else {
$re_counter = 0;
$counter = 0;
$subArrOne = [];
$subArrTwo = [];
for ($i = 0; $i <= $index; $i++) {
$subArrOne[] = $arr[$i];
}
for ($i = $index + 1; $i < count($arr); $i++) {
$subArrTwo[] = $arr[$i];
}
//var_dump($subArrOne);
//var_dump($subArrTwo);
// Re-populate original arr with new values
for ($i = $index + 1; $i < count($arr); $i++) {
$arr[$re_counter] = $subArrTwo[$counter];
$re_counter++;
$counter++;
}
$counter = 0;
for ($i = 0; $i <= $index; $i++) {
$arr[$re_counter] = $subArrOne[$counter];
$re_counter++;
$counter++;
}
//var_dump($arr);
}
}
// END exchange {index}
// BEGIN max even/odd
// BEGIN min even/odd
if ($commands[0] == "max" || $commands[0] == "min") {
$command = $commands[0];
$mode = $commands[1];
// BEGIN max
if ($command == "max") {
if ($mode == "even") {
$maxEven = -999999;
$maxEvenIndex = 0;
$foundMaxEvenElement = false;
for($i = 0; $i < count($arr); $i++) {
if ($arr[$i] % 2 == 0) {
$currentNum = $arr[$i];
if ($currentNum >= $maxEven) {
$maxEven = $currentNum;
$maxEvenIndex = $i;
$foundMaxEvenElement = true;
}
}
}
if ($foundMaxEvenElement == true) {
echo $maxEvenIndex . PHP_EOL;
} else {
echo "No matches" . PHP_EOL;
}
} else if ($mode == "odd") {
$maxOdd = -999999;
$maxOddIndex = 0;
$foundMaxOddElement = false;
for($i = 0; $i < count($arr); $i++) {
if (!($arr[$i] % 2 == 0)) {
$currentNum = $arr[$i];
if ($currentNum >= $maxOdd) {
$maxOdd = $currentNum;
$maxOddIndex = $i;
$foundMaxOddElement = true;
}
}
}
if ($foundMaxOddElement == true) {
echo $maxOddIndex . PHP_EOL;
} else {
echo "No matches" . PHP_EOL;
}
}
}
// END max
// BEGIN min
if ($command == "min") {
if ($mode == "even") {
$minEven = 999999;
$minEvenIndex = 0;
$foundMinEvenElement = false;
for($i = 0; $i < count($arr); $i++) {
if ($arr[$i] % 2 == 0) {
$currentNum = $arr[$i];
if ($currentNum <= $minEven) {
$minEven = $currentNum;
$minEvenIndex = $i;
$foundMinEvenElement = true;
}
}
}
if ($foundMinEvenElement == true) {
echo $minEvenIndex . PHP_EOL;
} else {
echo "No matches" . PHP_EOL;
}
} else if ($mode == "odd") {
$minOdd = 999999;
$minOddIndex = 0;
$foundMinOddElement = false;
for($i = 0; $i < count($arr); $i++) {
if (!($arr[$i] % 2 == 0)) {
$currentNum = $arr[$i];
if ($currentNum <= $minOdd) {
$minOdd = $currentNum;
$minOddIndex = $i;
$foundMinOddElement = true;
}
}
}
if ($foundMinOddElement == true) {
echo $minOddIndex . PHP_EOL;
} else {
echo "No matches" . PHP_EOL;
}
}
}
// END min
}
// END max even/odd
// END min even/odd
// BEGIN first count even/odd
// BEGIN last count even/odd
if ($commands[0] == "first" || $commands[0] == "last") {
$command = $commands[0];
$count = intval($commands[1]);
$mode = $commands[2];
if ($command == "first") {
if ($count == 0) {
echo "[]" . PHP_EOL;
} else if ($count > count($arr)) {
echo "Invalid count" . PHP_EOL;
} else {
if ($mode == "even") {
$firstEvenArr = [];
for ($i = 0; $i < count($arr); $i++) {
if ($arr[$i] % 2 == 0) {
$firstEvenArr[] = $arr[$i];
}
}
if ($count > count($firstEvenArr)) {
$count = count($firstEvenArr);
}
echo "[";
for ($i = 0; $i < $count; $i++) {
if ($i < $count - 1) {
echo $firstEvenArr[$i] . ", ";
} else {
echo $firstEvenArr[$i];
}
}
echo "]" . PHP_EOL;
} else if ($mode == "odd") {
$firstOddArr = [];
for ($i = 0; $i < count($arr); $i++) {
if (!($arr[$i] % 2 == 0)) {
$firstOddArr[] = $arr[$i];
}
}
if ($count > count($firstOddArr)) {
$count = count($firstOddArr);
}
echo "[";
for ($i = 0; $i < $count; $i++) {
if ($i < $count - 1) {
echo $firstOddArr[$i] . ", ";
} else {
echo $firstOddArr[$i];
}
}
echo "]" . PHP_EOL;
}
}
} else if ($command = "last") {
if ($count == 0) {
echo "[]" . PHP_EOL;
} else if ($count > count($arr)) {
echo "Invalid count" . PHP_EOL;
} else {
if ($mode == "even") {
$lastEvenArr = [];
$reverseLastEvenArr = [];
$finalLastEvenArr = [];
$reverseFinalLastEvenArr = [];
for ($i = 0; $i < count($arr); $i++) {
if ($arr[$i] % 2 == 0) {
$lastEvenArr[] = $arr[$i];
}
}
for ($i = count($lastEvenArr) - 1; $i >= 0; $i--) {
$reverseLastEvenArr[] = $lastEvenArr[$i];
}
if ($count > count($lastEvenArr)) {
$count = count($lastEvenArr);
}
for ($i = 0; $i < $count; $i++) {
$finalLastEvenArr[] = $reverseLastEvenArr[$i];
}
for ($i = count($finalLastEvenArr) - 1; $i >= 0; $i--) {
$reverseFinalLastEvenArr[] = $finalLastEvenArr[$i];
}
//var_dump($lastEvenArr);
//var_dump($reverseLastEvenArr);
//var_dump($finalLastEvenArr);
//var_dump($reverseFinalLastEvenArr);
echo "[";
for ($i = 0; $i < $count; $i++) {
if ($i < $count - 1) {
echo $reverseFinalLastEvenArr[$i] . ", ";
} else {
echo $reverseFinalLastEvenArr[$i];
}
}
echo "]" . PHP_EOL;
} else if ($mode == "odd") {
$lastOddArr = [];
$reverseLastOddArr = [];
$finalLastOddArr = [];
$reverseFinalLastOddArr = [];
for ($i = 0; $i < count($arr); $i++) {
if (!($arr[$i] % 2 == 0)) {
$lastOddArr[] = $arr[$i];
}
}
for ($i = count($lastOddArr) - 1; $i >= 0; $i--) {
$reverseLastOddArr[] = $lastOddArr[$i];
}
if ($count > count($lastOddArr)) {
$count = count($lastOddArr);
}
for ($i = 0; $i < $count; $i++) {
$finalLastOddArr[] = $reverseLastOddArr[$i];
}
for ($i = count($finalLastOddArr) - 1; $i >= 0; $i--) {
$reverseFinalLastOddArr[] = $finalLastOddArr[$i];
}
//var_dump($lastOddArr);
//var_dump($reverseLastOddArr);
//var_dump($finalLastOddArr);
//var_dump($reverseFinalLastOddArr);
echo "[";
for ($i = 0; $i < $count; $i++) {
if ($i < $count - 1) {
echo $reverseFinalLastOddArr[$i] . ", ";
} else {
echo $reverseFinalLastOddArr[$i];
}
}
echo "]" . PHP_EOL;
}
}
}
}
// END first count even/odd
// END last count even/odd
}
// END while loop
echo "[";
for($i = 0; $i < count($arr); $i++) {
if ($i < count($arr) - 1) {
echo $arr[$i] . ", ";
} else {
echo $arr[$i];
}
}
echo "]";
?>