Skip to content

Instantly share code, notes, and snippets.

@OscarGalindo
Last active October 25, 2023 11:51
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save OscarGalindo/6dcbc9782adfd3f03fc4 to your computer and use it in GitHub Desktop.
Save OscarGalindo/6dcbc9782adfd3f03fc4 to your computer and use it in GitHub Desktop.
Snail kata in Python @ Codewars
def snail(array):
results = []
while len(array) > 0:
# go right
results += array[0]
del array[0]
if len(array) > 0:
# go down
for i in array:
results += [i[-1]]
del i[-1]
# go left
if array[-1]:
results += array[-1][::-1]
del array[-1]
# go top
for i in reversed(array):
results += [i[0]]
del i[0]
return results
data = [[1, 2, 3, 4],
[12, 13, 14, 5],
[11, 16, 15, 6],
[10, 9, 8, 7]]
data2 = [[1, 2, 3],
[4, 5, 6],
[7, 8, 9]]
expected = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16]
expected2 = [1, 2, 3, 6, 9, 8, 7, 4, 5]
print(snail(data2))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment