-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwarcodewalk.py
44 lines (38 loc) · 1.01 KB
/
warcodewalk.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
def is_valid_walk(walk):
if len(walk) % 2 == 1 or len(walk)!=10:
return False
else:
x = []
y = []
for i in walk:
if i == 'n':
x.append(1)
elif i == 's':
x.append(-1)
elif i == 'w':
y.append(1)
elif i == 'e':
y.append(-1)
if sum(x) == 0 and sum(y) == 0:
return True
else:
return False
def isValidWalk(walk):
if len(walk) != 10:
return False
x = 0
y = 0
for direction in walk:
if direction == 'n':
y += 1
elif direction == 's':
y -= 1
elif direction == 'e':
x += 1
elif direction == 'w':
x -= 1
else:
return False
return x == 0 and y == 0
print(is_valid_walk(['n','s','n','s','n','s','n','s','n','s']))
print(isValidWalk(['n','s','n','s','n','s','n','s','n','s']))