8000 Nasa robot movement · premaseem/pythonLab@591bbee · GitHub
[go: up one dir, main page]

Skip to content

Commit 591bbee

Browse files
Aseem JainAseem Jain
authored andcommitted
Nasa robot movement
1 parent 9c7f7f1 commit 591bbee

File tree

1 file changed

+137
-0
lines changed

1 file changed

+137
-0
lines changed
Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
"""
2+
@Author: Aseem Jain
3+
@Linkedin: https://www.linkedin.com/in/premaseem/
4+
@Github: https://github.com/premaseem/pythonLab/tree/master/challenge
5+
6+
"""
7+
# NASA is sending a robot to Moon's surface in an unmanned mission. The robot will be controlled remotely by issuing simple commands to maneuver it.
8+
9+
# The robot's position is represented by a combination of an x and y co-ordinates and a letter representing one of the four directions in which it is facing. An example position might be 0, 0, N, which means the robot is in the bottom left corner and facing North.
10+
11+
# Directions Key: E = East, W = West, S = South, N = North
12+
13+
# Grid : 0,0 is at bottom left.
14+
15+
# ........................(+x,+y)
16+
# | |
17+
# | |
18+
# 0,0 ......................|
19+
20+
21+
# Nasa sends commands in the form of simple letters. L, R and.
22+
23+
# L : Rotates the robot 90 degrees Counter-clockwise in it's current position itself
24+
# R : Rotates the robot 90 degrees clockwise in it's current position itself
25+
# M : Moves the robot 1 unit forward in whatever direction it is facing
26+
27+
# Important NOTE: The robot will stay in its position if you try to move beyond any edge of the grid
28+
29+
# INPUT:
30+
31+
# (max-X-coordinate, max-Y-coordinate, current-position-and-direction, [list of commands])
32+
33+
# Example: solution(5, 5, "1 2 N", ["R", "M", "L", "M", "R"])
34+
35+
# The grid goes from (0,0 to 5,5)
36+
37+
###### Example for (5,5) grid:
38+
39+
# 0,5 1,5 2,5 3,5 4,5 5,5
40+
41+
# 0,4 1,4 2,4 3,4 4,4 5,4
42+
43+
# 0,3 1,3 2,3 3,3 4,3 5,3
44+
45+
# 0,2 1,2 2,2 3,2 4,2 5,2
46+
47+
# 0,1 1,1 2,1 3,1 4,1 5,1
48+
49+
# 0,0 1,0 2,0 3,0 4,0 5,0
50+
51+
52+
###### Directions for reference
53+
54+
# N
55+
# |
56+
# |
57+
# W ----- ----- E
58+
# |
59+
# |
60+
# S
61+
62+
# EXPECTED OUTPUT:
63+
64+
# Final position and direction of the robot (X, Y, Direction) as a string
65+
66+
# Example: "2 3 E"
67+
68+
# Explanation:
69+
70+
# Initial position: 1,2,N
71+
# R : Turn Right to point in East direction
72+
# M : Move 1 grid point to 2,2
73+
# L : Turn Left to point in North direction
74+
# M : Move 1 grid point to 2, 3
75+
# R : Turn Right to point in East direction
76+
# Final Position: 2 3 E
77+
78+
79+
80+
81+
# --------------------- Python -------------------
82+
83+
def sol(grid_max_x, grid_max_y, initial_position, commands):
84+
sp = initial_position.split(" ")
85+
x = int(sp[0])
86+
y = int(sp[1])
87+
p = sp[2]
88+
89+
pm = {}
90+
pm["N"] = [0,1]
91+
pm["S"] = [0,-1]
92+
pm["E"] = [1,0]
93+
pm["W"] = [-1,0]
94+
95+
d = ["N","E","S","W"]
96+
cd = d.index(p)
97+
for c in commands:
98+
cd = d.index(d[cd])
99+
100+
if c == "L":
101+
cd = cd - 1
102+
if cd < 0:
103+
cd = 3
104+
105+
elif c == "R":
106+
cd = cd + 1
107+
if cd > 3:
108+
cd = 0
109+
110+
else:
111+
112+
delta = pm.get(d[cd])
113+
if x < grid_max_x:
114+
x = x + delta[0]
115+
if x == -1 :
116+
x = 0
117+
if y < grid_max_y and y >= 0 :
118+
y = y + delta[1]
119+
if y == -1:
120+
y = 0
121+
122+
# print("output", x,y,d[cd])
123+
124+
return "{} {} {}".format(str(x),str(y),str(d[cd]))
125+
126+
127+
test_data = [
128+
((5, 5, "1 2 N", ["R", "M", "L", "M", "R"]), "2 3 E"),
129+
( (5, 5, "1 2 N", ["L", "M", "L", "M", "L", "M", "L", "M", "M"] ), "1 3 N"),
130+
( (5, 5, "1 4 N", ["L", "M", "M", "M", "L", "M", "R"] ), "0 3 W"),
131+
]
132+
133+
for given, expected in test_data:
134+
print(f" Test with given {given} and expected = {expected}", end=" == ")
135+
actual = sol(*given)
136+
print("actual", sol(*given))
137+
assert actual == expected

0 commit comments

Comments
 (0)
0