-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproblem1041.py
42 lines (42 loc) · 1.45 KB
/
problem1041.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
class Solution(object):
def execute_instructions(self, pos, heading, instructions):
for order in instructions:
if order == 'G':
if heading == 'N':
pos = (pos[0] - 1, pos[1])
elif heading == 'W':
pos = (pos[0], pos[1] - 1)
elif heading == 'E':
pos = (pos[0], pos[1] + 1)
elif heading == 'S':
pos = (pos[0] + 1, pos[1])
elif order == 'L':
if heading == 'N':
heading = 'W'
elif heading == 'W':
heading = 'S'
elif heading == 'E':
heading = 'N'
elif heading == 'S':
heading = 'E'
elif order == 'R':
if heading == 'N':
heading = 'E'
elif heading == 'W':
heading = 'N'
elif heading == 'E':
heading = 'S'
elif heading == 'S':
heading = 'W'
return heading, pos
def isRobotBounded(self, instructions):
"""
:type instructions: str
:rtype: bool
"""
heading = 'N'
pos = (0, 0)
for testcase in range(0, 4):
heading, pos = self.execute_instructions(pos, heading, instructions)
if pos == (0, 0): return True
return False