Last active
February 26, 2018 23:10
-
-
Save dilanshah/78557b9991d21f054746d58e934a4743 to your computer and use it in GitHub Desktop.
return if move sequences qualifies as a valid circle
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
''' | |
Initially, there is a Robot at position (0, 0). Given a sequence of its | |
moves, judge if this robot makes a circle, which means it moves back to | |
the original place. | |
The move sequence is represented by a string. And each move is represent | |
by a character. The valid robot moves are R (Right), L (Left), U (Up) and | |
D (down). The output should be true or false representing whether the | |
robot makes a circle. | |
Example 1: | |
Input: "UD" | |
Output: true | |
Example 2: | |
Input: "LL" | |
Output: false | |
''' | |
class Solution: | |
def judgeCircle(self, moves): | |
initpos = [0,0] | |
pos = [0,0] | |
""" | |
:type moves: str | |
:rtype: bool | |
""" | |
# self.validMoves = { | |
# 'L' : 0, | |
# 'R' : 1, | |
# 'U' : 2, | |
# 'D' : 3 | |
# } | |
for move in moves: | |
if move is 'R': | |
pos[0] += 1 | |
elif move is 'L': | |
pos[0] -= 1 | |
elif move is 'U': | |
pos[1] += 1 | |
else: | |
pos[1] -= 1 | |
if pos == initpos: | |
print("True") | |
return True | |
print("False") | |
return False | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment