-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSnail.py
54 lines (42 loc) · 1.14 KB
/
Snail.py
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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# Codewar : Snail Sort
def snail(array):
inputs = array
result = []
print(len(inputs))
if len(inputs)<= 1:
for input in inputs:
return input
else:
i = 0
start_i = 0
start_j = 0
end_i = len(inputs)
end_j = len(inputs)
while len(result) != len(inputs)*len(inputs):
for j in range(start_j, end_j):
result.append(inputs[i][j])
start_i += 1
for i in range(start_i, end_i):
result.append(inputs[i][j])
end_j -= 1
for j in reversed(range(start_j, end_j)):
result.append(inputs[i][j])
end_i -= 1
for i in reversed(range(start_i, end_i)):
result.append(inputs[i][j])
start_j += 1
print(result)
return result
array = [[1]]
expected = [1]
print(snail(array)== expected)
array = [[1,2,3],
[4,5,6],
[7,8,9]]
expected = [1,2,3,6,9,8,7,4,5]
print(snail(array)== expected)
array = [[1,2,3],
[8,9,4],
[7,6,5]]
expected = [1,2,3,4,5,6,7,8,9]
print(snail(array) == expected)