File tree Expand file tree Collapse file tree 1 file changed +20
-17
lines changed
exercises/40-compute-robot-distance Expand file tree Collapse file tree 1 file changed +20
-17
lines changed Original file line number Diff line number Diff line change 1
1
# Your code here
2
- import math
2
+ def compute_robot_distance (movements ):
3
+ x , y = 0 , 0
3
4
4
- def compute_robot_distance (props ):
5
- pos = [0 ,0 ]
6
- new_prop = props .split (" " )
7
- for x in range (len (new_prop )):
8
- if new_prop [x ].upper () == 'UP' :
9
- pos [0 ]+= int (new_prop [x + 1 ])
10
- elif new_prop [x ].upper () == 'DOWN' :
11
- pos [0 ]-= int (new_prop [x + 1 ])
12
- elif new_prop [x ].upper () == 'LEFT' :
13
- pos [1 ]-= int (new_prop [x + 1 ])
14
- elif new_prop [x ].upper () == 'RIGHT' :
15
- pos [1 ]+= int (new_prop [x + 1 ])
16
- else :
17
- None
18
- return (int (round (math .sqrt (pos [1 ]** 2 + pos [0 ]** 2 ))))
5
+ for move in movements :
6
+ direction , steps = move .split ()
7
+ steps = int (steps )
19
8
20
- print (compute_robot_distance ("UP 5 DOWN 3 LEFT 3 RIGHT 2" ))
9
+ if direction == "UP" :
10
+ y += steps
11
+ elif direction == "DOWN" :
12
+ y -= steps
13
+ elif direction == "LEFT" :
14
+ x -= steps
15
+ elif direction == "RIGHT" :
16
+ x += steps
17
+
18
+ distance = (x ** 2 + y ** 2 )** 0.5
19
+ rounded_distance = round (distance )
20
+
21
+ return rounded_distance
22
+
23
+ print (compute_robot_distance (["UP 5" , "DOWN 3" , "LEFT 3" , "RIGHT 2" ]))
You can’t perform that action at this time.
0 commit comments