-
Notifications
You must be signed in to change notification settings - Fork 20
/
robotboundedincircle.py
35 lines (28 loc) · 1.33 KB
/
robotboundedincircle.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
'''
On an infinite plane, a robot initially stands at (0, 0) and faces north. Note that:
The north direction is the positive direction of the y-axis.
The south direction is the negative direction of the y-axis.
The east direction is the positive direction of the x-axis.
The west direction is the negative direction of the x-axis.
The robot can receive one of three instructions:
"G": go straight 1 unit.
"L": turn 90 degrees to the left (i.e., anti-clockwise direction).
"R": turn 90 degrees to the right (i.e., clockwise direction).
The robot performs the instructions given in order, and repeats them forever.
Return true if and only if there exists a circle in the plane such that the robot never leaves the circle.
'''
class Solution:
def isRobotBounded(self, instructions: str) -> bool:
directionX = 0
directionY = 1
positionX = 0
positionY = 0
for inst in instructions:
if inst == "G":
positionX = positionX + directionX
positionY = positionY + directionY
elif inst == "L":
(directionX, directionY) = (- 1 * directionY, directionX)
elif inst == "R":
(directionX, directionY) = (directionY, -1 * directionX)
return (positionX,positionY) == (0,0) or (directionX,directionY) != (0,1)