Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
<?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