Skip to content

Commit 63f3c3f

Browse files
authored
Create Amazon-RoverControl.md
1 parent cbf7e6f commit 63f3c3f

File tree

1 file changed

+113
-0
lines changed

1 file changed

+113
-0
lines changed

Amazon-RoverControl.md

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
## Amazon | OA | Rover Control
2+
3+
Amazon Online Assessment: https://leetcode.com/discuss/interview-question/985703/Amazon-or-OA-or-Rover-Control
4+
5+
A Mars rover is directed to move within a square matrix. It accepts a sequence of
6+
commands to move in any of the four directions from each cell: [UP, DOWN, LEFT or
7+
RIGHT]. The rover starts from cell 0. and may not move diagonally or outside of
8+
the boundary.
9+
10+
Each cell in the matrix has a position equal to:
11+
(row * size) + column
12+
where row and column are zero-indexed, size = row length of the matrix.
13+
14+
Return the final position of the rover after all moves.
15+
16+
Example
17+
n = 4
18+
commands = [RIGHT, UP, DOWN, LEFT, DOWN, DOWN]
19+
20+
The rover path is shown below.
21+
22+
0 1 2 3
23+
4 5 6 7
24+
8 9 10 11
25+
12 13 14 15
26+
27+
RIGHT: Rover moves to position 1
28+
UP: Position unchanged, as the move would take the rover out of the boundary.
29+
DOWN: Rover moves to Position 5.
30+
LEFT: Rover moves to position 4
31+
DOWN: Rover moves to position 8
32+
DOWN: The rover ends up in position 12.
33+
34+
The function returns 12.
35+
36+
Function Description
37+
Complete the function roverMove in the editor below.
38+
roverMove has the following parameter(s):
39+
int n: the size of the square matrix
40+
string cmds[m]: the commands
41+
42+
Returns
43+
int: the label of the cell the rover occupies after executing all commands
44+
45+
Constraints
46+
2 <= n <= 20
47+
1 <= |cmds| <= 20
48+
49+
Input Format For Custom Testing
50+
51+
Input from stdin will be processed as follows and passed to the function.
52+
The first line contains an integer, n, denoting the size of the square matrix.
53+
The next line contains an integer, m, the number of commands to follow.
54+
Each of the next m lines contains a command string, cmds[i].
55+
56+
Sample Input :
57+
STDIN Function
58+
59+
4 → n = 4
60+
5 → cmds [] size m = 5
61+
RIGHT → cmds = ['RIGHT', 'DOWN', 'LEFT', 'LEFT', 'DOWN']
62+
DOWN
63+
LEFT
64+
LEFT
65+
DOWN
66+
67+
Sample Output:
68+
8
69+
70+
Explanation:
71+
0 1 2 3
72+
4 5 6 7
73+
8 9 10 11
74+
12 13 14 15
75+
Rover starts at position 0
76+
RIGHT → pos 1
77+
DN → pos 5
78+
LEFT → pos 4
79+
LEFT → pos 4, No effect
80+
DOWN → pos 8
81+
'''
82+
83+
84+
```python
85+
def rover(n, cmds):
86+
pos = 0
87+
for c in cmds:
88+
if c == 'UP':
89+
if pos//n >=1:
90+
pos -= n
91+
elif c == 'DOWN':
92+
if pos//n < n-1:
93+
pos += n
94+
elif c == 'RIGHT':
95+
if (pos % n) < n-1:
96+
pos +=1
97+
elif c == 'LEFT':
98+
if (pos % n) >=1:
99+
pos -= 1
100+
return pos
101+
102+
n = 4
103+
cmds = ['RIGHT', 'DOWN', 'LEFT', 'LEFT', 'DOWN'] # 8
104+
cmds2 = ['RIGHT', 'UP', 'DOWN', 'LEFT', 'DOWN', 'DOWN'] # 12
105+
expected_output2 = 8
106+
expected_output2 = 12
107+
output = rover(n, cmds)
108+
output2 = rover(n, cmds2)
109+
print(output)
110+
print(output == expected_output)
111+
print(output2)
112+
print(output2 == expected_output2)
113+
```

0 commit comments

Comments
 (0)