-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript1.py
More file actions
59 lines (54 loc) · 1.38 KB
/
script1.py
File metadata and controls
59 lines (54 loc) · 1.38 KB
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
def snail_path(matrix):
if len(matrix) == 0:
return False
sum_list = []
i = 0
j_max = -1
i_max = -1
while len(matrix) >= 1:
for item in matrix[i]:
sum_list.append(item)
del matrix[i]
for item in matrix:
if item != []:
sum_list.append(item[j_max])
del item[j_max]
else:
break
if matrix != []:
matrix[i_max].reverse()
for item in matrix[i_max]:
sum_list.append(item)
del matrix[i_max]
else:
break
matrix.reverse()
for item in matrix:
if item != []:
sum_list.append(item[i])
del item[i]
else:
break
matrix.reverse()
if len(matrix) == 0:
break
return sum_list
print(not snail_path([]))
print(not snail_path([[]]))
print(snail_path([[0]]) == [0])
print(snail_path([[1, 2, 3]]) == [1, 2, 3])
print(snail_path([[1], [2], [3], [4]]) == [1, 2, 3, 4])
print(snail_path([
[1, 2],
[3, 4],
]) == [1, 2, 4, 3])
print(snail_path([
[1, 2, 3, 4],
[5, 6, 7, 8],
[9, 10, 11, 12],
]) == [1, 2, 3, 4, 8, 12, 11, 10, 9, 5, 6, 7])
print(snail_path([
[None, 0, True],
[-1, '', False],
[[], 'foo', object],
]) == [None, 0, True, False, object, 'foo', [], -1, ''])