-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path#4_kyu_Snail.py
59 lines (50 loc) · 1.6 KB
/
#4_kyu_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
55
56
57
58
59
Description:
"""
Snail Sort
Given an n x n array, return the array elements arranged from outermost elements to the middle element, traveling clockwise.
array = [[1,2,3],
[4,5,6],
[7,8,9]]
snail(array) #=> [1,2,3,6,9,8,7,4,5]For better understanding, please follow the numbers of the next array consecutively:
array = [[1,2,3],
[8,9,4],
[7,6,5]]
snail(array) #=> [1,2,3,4,5,6,7,8,9]This image will illustrate things more clearly:
NOTE: The idea is not sort the elements from the lowest value to the highest; the idea is to traverse the 2-d array in a clockwise snailshell pattern.
NOTE 2: The 0x0 (empty matrix) is represented as [[]]
"""
My codes:
def snail(array):
if len(array) == 1:
return array[0]
print(array)
left,right,top,bottom = -1,len(array[0])-1,0,len(array)-1
x,y,t_r,t_u = 0,1,1,0
ans = []
ans.append(array[0][0])
while left != right or bottom != top :
ans.append(array[x][y])
if y == right and x == top:
t_u = 1
t_r = 0
left += 1
elif y == right and x == bottom:
t_u = 0
t_r = -1
top += 1
elif y == left and x == bottom:
t_u = -1
t_r = 0
right -= 1
elif y == left and x == top:
t_u = 0
t_r = 1
bottom -= 1
x += t_u
y += t_r
return ans
Others codes:
def snail(array):
return list(array[0]) + snail(zip(*array[1:])[::-1]) if array else []
def snail(array):
return list(array[0]) + snail(zip(*array[1:])[::-1]) if array else []