-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsnail.py
More file actions
42 lines (41 loc) · 996 Bytes
/
snail.py
File metadata and controls
42 lines (41 loc) · 996 Bytes
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
def snail(snail_map):
res=[]
count=0
x=0
y=0
maxnum=len(snail_map)-1 #max value of x and y
minnum=0 #minimum value of x and y
numchanged=False
while len(res)<len(snail_map[0])**2:
if numchanged==False:
res.append(snail_map[y][x])
else:
numchanged=False
num=count%4
if num==0:
if x==maxnum:
count+=1
numchanged=True
else:
x+=1
if num==1:
if y==maxnum:
count+=1
maxnum-=1
numchanged=True
else:
y+=1
if num==2:
if x==minnum:
count+=1
minnum+=1
numchanged=True
else:
x-=1
if num==3:
if y==minnum:
count+=1
numchanged=True
else:
y-=1
return res