Skip to content

Commit eb1fa40

Browse files
authored
Update walking-robot-simulation-ii.py
1 parent 9802d45 commit eb1fa40

File tree

1 file changed

+53
-0
lines changed

1 file changed

+53
-0
lines changed

Python/walking-robot-simulation-ii.py

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,59 @@
33

44
class Robot(object):
55

6+
def __init__(self, width, height):
7+
"""
8+
:type width: int
9+
:type height: int
10+
"""
11+
self.__w = width
12+
self.__h = height
13+
self.__curr = 0
14+
15+
def move(self, num):
16+
"""
17+
:type num: int
18+
:rtype: None
19+
"""
20+
self.__curr += num
21+
22+
def getPos(self):
23+
"""
24+
:rtype: List[int]
25+
"""
26+
n = self.__curr % (2*((self.__w-1)+(self.__h-1)))
27+
if n < self.__w:
28+
return [n, 0]
29+
n -= self.__w-1
30+
if n < self.__h:
31+
return [self.__w-1, n]
32+
n -= self.__h-1
33+
if n < self.__w:
34+
return [(self.__w-1)-n, self.__h-1]
35+
n -= self.__w-1
36+
return [0, (self.__h-1)-n]
37+
38+
def getDir(self):
39+
"""
40+
:rtype: str
41+
"""
42+
n = self.__curr % (2*((self.__w-1)+(self.__h-1)))
43+
if n < self.__w:
44+
return "South" if n == 0 and self.__curr else "East"
45+
n -= self.__w-1
46+
if n < self.__h:
47+
return "North"
48+
n -= self.__h-1
49+
if n < self.__w:
50+
return "West"
51+
n -= self.__w-1
52+
return "South"
53+
54+
55+
# Time: O(1)
56+
# Space: O(1)
57+
class Robot2(object):
58+
659
def __init__(self, width, height):
760
"""
861
:type width: int

0 commit comments

Comments
 (0)