-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1041_medium_[sense]_robot_bounded_in_circle.py
58 lines (48 loc) · 1.67 KB
/
1041_medium_[sense]_robot_bounded_in_circle.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
from typing import List, Tuple
UP, DOWN, LEFT, RIGHT = "U", "D", "L", "R"
class Solution:
def isRobotBounded(self, instructions: str) -> bool:
location, direction = [0, 0], UP
while True:
for instruction in instructions:
location, direction = self._move(instruction, location, direction)
if direction == UP and location == [0, 0]:
return True
elif direction == UP:
return False
def _move(self, instruction: str, location: List[int], direction: str) -> Tuple[List[int], str]:
if direction == UP:
if instruction == "G":
location[1] += 1
elif instruction == "L":
direction = LEFT
else:
direction = RIGHT
elif direction == DOWN:
if instruction == "G":
location[1] -= 1
elif instruction == "L":
direction = RIGHT
else:
direction = LEFT
elif direction == LEFT:
if instruction == "G":
location[0] -= 1
elif instruction == "L":
direction = DOWN
else:
direction = UP
else:
if instruction == "G":
location[0] += 1
elif instruction == "L":
direction = UP
else:
direction = DOWN
return location, direction
if __name__ == '__main__':
s = Solution()
assert s.isRobotBounded("G") is False
assert s.isRobotBounded("GGLLGG") is True
assert s.isRobotBounded("GG") is False
assert s.isRobotBounded("GL") is True