Last active
August 29, 2015 13:57
-
-
Save AshleyPinner/9414861 to your computer and use it in GitHub Desktop.
This file contains 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 | |
/* | |
Challenge 7: Snail print an array | |
Rules:: | |
1. No Exceptions or PHP errors / warnings notices allowed | |
2. No redefining the test arrays. | |
3. Your function must be able to cope with n by n arrays as input | |
4. Your output must be a single string | |
Hints: | |
1. Recursion might be your option. See 3. | |
2. PHP has array functions. I highly suggest them :) | |
3. Recursion might be your option. See 1. | |
Explanation: | |
Assume you have an n by n array that looks like such: | |
[[1, 2, 3], | |
[4, 5, 6], | |
[7, 8, 9]] | |
Snail printing is where you iterate through the array, and print the values that are on the outermost edge, working inwards. | |
So for the above array, your output would be 123698745 | |
Going larger, a 4x4 example: | |
[[1, 2, 3, 4], | |
[5, 6, 7, 8], | |
[9, 8, 7, 6], | |
[5, 4, 3, 2]] | |
Your output would be 1234862345956778 | |
*/ | |
function snail($input) | |
{ | |
// Start editing here | |
//end editing here | |
return $output; | |
} | |
$testArrayOne = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]; | |
$result = snail($testArrayOne); | |
if($result != '123698745') | |
{ | |
die('Failure of output: ' .$result ); | |
} | |
$testArrayTwo = [[1, 2, 3, 4], [5, 6, 7, 8], [9, 8, 7, 6], [5, 4, 3, 2]]; | |
$result = snail($testArrayTwo); | |
if($result != '1234862345956778') | |
{ | |
die('Failure of output: ' .$result ); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment