Last active
August 29, 2015 13:56
-
-
Save Micka33/8917297 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
def snail(array) | |
x = 0 | |
y = 0 | |
done = [] | |
indexes = [[1,0],[0,1],[-1,0],[0,-1]].cycle | |
delta = indexes.next | |
res = [] | |
array.flatten.length.times do |n| | |
res << array[y][x] | |
done << x.to_s+y.to_s | |
x = x+delta[0] | |
y = y+delta[1] | |
if (done.include?(x.to_s+y.to_s) || | |
array[y].nil? || array[y][x].nil? || | |
y < 0 || x < 0) | |
x = x-delta[0] | |
y = y-delta[1] | |
delta = indexes.next | |
x = x+delta[0] | |
y = y+delta[1] | |
end | |
end | |
res | |
end | |
def snail(array) | |
array.empty? ? [] : array.shift + snail(array.transpose.reverse) | |
end | |
#Test | |
def test input , expected | |
array = [[ 1, 2, 3, 4, 5], | |
[ 6, 7, 8, 9,10], | |
[11,12,13,14,15], | |
[16,17,18,19,20], | |
[21,22,23,24,25]] | |
expected = [1,2,3,4,5,10,15,20,25,24,23,22,21,16,11,6,7,8,9,14,19,18,17,12,13] | |
resut = snail(array) | |
expected == result | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment