Last active
December 19, 2022 12:47
-
-
Save Kaapiii/b78116341de9fcd16a62792945c11537 to your computer and use it in GitHub Desktop.
PHP exercises with arrays
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* -------------------------------------------------------------- | |
* Programming exercises arrays and loops | |
* -------------------------------------------------------------- | |
* source: https://gist.github.com/Kaapiii/b78116341de9fcd16a62792945c11537 | |
* | |
* Task: solve all tasks by using one of the four loop types. The same loop type can be used for all functions. | |
* | |
* Execute file: "php -f array_exercises.php" | |
*/ | |
// Test data for exercises | |
$testA = [4, 6, 0, 3, 7, 10, 4, 5, 6, 9, 3, 8, 9, 0, 3, 5, 3, 4, 9, 4]; | |
$testB = [3, 14, 15, 13, 5, 3, 14, 8, 12, 11, 13, 14, 15, 2, 3, 15, 8, 1, 12, 0, 6, 9, 11, 0, 14, 5, 2, 2, 15, 13]; | |
$testC = [2,3,4,5,6,7,8]; | |
/** | |
* --------------------------------------- | |
* Examples functions with different loops | |
* --------------------------------------- | |
*/ | |
function echoElements(array $input) | |
{ | |
$max = count($input); | |
// -------------------- | |
// Common loops | |
// -------------------- | |
// Desc: These loops will terminate and won't loop forever. | |
// for loop | |
for ($i = 0; $i < $max; $i++) { | |
echo $input[$i]; | |
} | |
echo PHP_EOL; | |
// foreach loop | |
foreach ($input as $item) { | |
echo $item; | |
} | |
echo PHP_EOL; | |
// -------------------- | |
// Less common loops | |
// -------------------- | |
// Desc: These loops can loop forever, if the condition and increment are set wrong. | |
// while loop | |
$i = 0; | |
while ($i < $max) { | |
echo $input[$i]; | |
$i++; // increment i, otherwise it will loop forever | |
} | |
echo PHP_EOL; | |
// do while | |
$j = 0; | |
do { | |
echo $input[$j]; | |
$j++; // increment i, otherwise it will loop forever | |
} while ($j < $max); | |
echo PHP_EOL; | |
} | |
// Output/Echo with loops | |
echoElements($testB); | |
// Output/echo with created helper function | |
println($testB); | |
/** | |
* ------------------------------- | |
* Exercises / Tasks | |
* ------------------------------- | |
*/ | |
/** | |
* Find the lowest number in array | |
* | |
* @param array $arrayA | |
* @return int | |
*/ | |
function findLowest(array $arrayA): int | |
{ | |
// @Todo Code here and update return type | |
return 0; | |
} | |
//$result1 = findLowest($testA); | |
//$result2 = findLowest($testB); | |
//println($result1); | |
//println($result2); | |
// shorter method | |
// println(findLowest($testA)); | |
/** | |
* Find the highest number in array | |
* | |
* @param array $arrayA | |
* @return int | |
*/ | |
function findHighest(array $arrayA): int | |
{ | |
// @Todo Code here and update return type | |
return 0; | |
} | |
//println(findHighest($testA)); | |
//println(findHighest($testB)); | |
/** | |
* Find combine two arrays and return the combined result array | |
* | |
* @param array $arrayA | |
* @param array $arrayB | |
* @return array | |
*/ | |
function combineArrays(array $arrayA, array $arrayB): array | |
{ | |
// @Todo Code here and update return type | |
return []; | |
} | |
//println(combineArrays($testA, $testB)); | |
/** | |
* Find combine two arrays and count elements number in array | |
* | |
* @param array $arrayA | |
* @param array $arrayB | |
* @return int | |
*/ | |
function combineArraysAndCountElements(array $arrayA, array $arrayB): int | |
{ | |
// @Todo Code here and update return type | |
return 0; | |
} | |
//println(combineArraysAndCountElements($testA, $testB)); | |
/** | |
* Find combine two arrays and sum all values | |
* | |
* @param array $arrayA | |
* @param array $arrayB | |
* @return int | |
*/ | |
function combineArraysAndSum(array $arrayA, array $arrayB): int | |
{ | |
// @Todo Code here and update return type | |
return 0; | |
} | |
//println(combineArraysAndSum($testA, $testB)); | |
/** | |
* Find all even numbers | |
* | |
* @param array $arrayA | |
* @return int | |
*/ | |
function findAllEvenNumbers(array $arrayA): int | |
{ | |
// @Todo Code here and update return type | |
return 0; | |
} | |
//println(findAllEvenNumbers($testA)); | |
//println(findAllEvenNumbers($testB)); | |
/** | |
* Find all even numbers | |
* | |
* @param array $arrayA | |
* @return int | |
*/ | |
function findAllOddNumbers(array $arrayA): int | |
{ | |
// @Todo Code here and update return type | |
return 0; | |
} | |
//println(findAllOddNumbers($testA)); | |
//println(findAllOddNumbers($testB)); | |
//------------------------------------- | |
// Find the errors and correct them | |
//------------------------------------- | |
/** | |
* This function has a small error. Find it and fix it. | |
* Runtime Message: "PHP Notice: Undefined offset: 30 in ...." | |
* | |
* Hint: use the debugger | |
*/ | |
function findTheErrorRemoveTheNotice(array $input) | |
{ | |
$max = count($input); | |
// while loop | |
$i = 0; | |
while ($i < $max) { | |
echo $input[$i]; | |
$i++; // increment i, otherwise it will loop forever | |
} | |
echo PHP_EOL; | |
// do while | |
do { | |
echo $input[$i]; | |
$i++; // increment i, otherwise it will loop forever | |
} while ($i < $max); | |
echo PHP_EOL; | |
} | |
findTheErrorRemoveTheNotice($testB); | |
//------------------------------------- | |
// Advanced - Sorting | |
//------------------------------------- | |
/** | |
* Find combine two arrays and order values ascending | |
* | |
* @param array $arrayA | |
* @param array $arrayB | |
* @return array | |
*/ | |
function combineArraysAndSortAsc(array $arrayA, array $arrayB): array | |
{ | |
// @Todo Code here and update return type | |
return []; | |
} | |
//println(combineArraysAndSortAsc($testA, $testB)); | |
/** | |
* Find combine two arrays and order values descending | |
* | |
* @param array $arrayA | |
* @param array $arrayB | |
* @return array | |
*/ | |
function combineArraysAndSortDesc(array $arrayA, array $arrayB): array | |
{ | |
// @Todo Code here and update return type | |
return []; | |
} | |
//println(combineArraysAndSortDesc($testA, $testB)); | |
/** | |
* Add a new number at beginning of the array | |
* | |
* @param array $arrayA | |
* @param int $newNumber | |
* @return array | |
*/ | |
function addAsFirstElement(array $arrayA, int $newNumber): array | |
{ | |
// @Todo Code here and update return type | |
return []; // should return [1,2,3,4,5,6,7,8] | |
} | |
//println(addAsFirstElement($testA, 1)); | |
/** | |
* Add a new number at the of the array | |
* | |
* @param array $arrayA | |
* @param array $arrayB | |
* @return array | |
*/ | |
function addAsLastElement(array $arrayA, int $newNumber): array | |
{ | |
// @Todo Code here and update return type | |
return []; // should return [2,3,4,5,6,7,8,9] | |
} | |
//println(addAsLastElement($testA, 9)); | |
/** | |
* Add a new number at the given position and reorder the other numbers | |
* | |
* @param array $arrayA | |
* @param int $newNumber | |
* @param int $position | |
* @return array | |
*/ | |
function addElementAtPosition(array $arrayA, int $newNumber, int $position): array | |
{ | |
// @Todo Code here and update return type | |
return []; // should return [2,3,4,100,5,6,7,8,9] -> note that array index start with index "0". | |
} | |
//println(addElementAtPosition($testA, 100, 3)); | |
/** | |
* Replace a element/value at a given position | |
* | |
* @param array $arrayA | |
* @param int $newNumber | |
* @param int $position | |
* @return array | |
*/ | |
function replaceElementAtPosition(array $arrayA, int $newNumber, int $position): array | |
{ | |
// @Todo Code here and update return type | |
return []; // should return [4, 6, 0, 3, 7, 100, 4, 5, 6, 9, 3, 8, 9, 0, 3, 5, 3, 4, 9, 4] | |
} | |
//println(replaceElementAtPosition($testA, 100, 6)); | |
/** | |
* Replace all occurrences of a value with a new value | |
* | |
* @param array $arrayA | |
* @param int $searchValue | |
* @param int $replaceValue | |
* @return array | |
*/ | |
function replaceAllWith(array $arrayA, int $searchValue, int $replaceValue): array | |
{ | |
// @Todo Code here and update return type | |
return []; // should return [100, 6, 0, 3, 7, 10, 100, 5, 6, 9, 3, 8, 9, 0, 3, 5, 3, 100, 9, 100]; | |
} | |
//println(replaceAllWith($testA, 4, 100)); | |
//------------------------------------- | |
// Helper methods | |
//------------------------------------- | |
function println($input): void | |
{ | |
if (is_array($input)) { | |
echo implode(', ', $input) . PHP_EOL; | |
return; | |
} | |
echo $input . PHP_EOL; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment