Created
December 3, 2013 00:45
-
-
Save trappist/7761947 to your computer and use it in GitHub Desktop.
Consume a 2d array in a "spiral"
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
#!/usr/bin/env ruby | |
def spiral(array,ans=[]) | |
return ans unless array.any? | |
ans += array.shift | |
spiral(array.transpose.reverse, ans) | |
end | |
array = [ | |
[1,2,3,1], | |
[4,5,6,4], | |
[7,8,9,7], | |
[7,8,9,7] | |
] | |
puts spiral(array).inspect |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment