Can anyone find the bug in my code? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Can anyone find the bug in my code?

You're given a representation of a 5x5 2D map, and if you can only move left, right, up, or down, determine how many moves you would have to make to get between two points on the map. Task: Determine the total number of moves that are needed between two points on a map. The points that you move between are marked with a P and the spaces in between are marked with X. Input Format: A string that represents the 2D space starting at the top left. Each level from top to bottom is separated by a comma. Output Format: An integer that represents the total number of moves that you had to make. Sample Input: XPXXX,XXXXX,XXXXX,XXXPX,XXXXX Sample Output: 5 My code (in Python) is as follows : import math map=input() index=[] for i in range(len(map)): if(map[i]=='P'): index.append(i) difference = index[1]-index[0] up_down = math.floor(difference/6) right_left = (difference%6) total_steps = up_down + right_left print(total_steps)

12th Sep 2023, 8:37 AM
Turles
Turles - avatar
3 Answers
+ 8
Turles , assuming the input string is: 'XXXXP,PXXXX,XXXXX,XXXXX,XXXXX' your code returms 2 as result. but this is not correct. it has to be 5. one step down, and 4 left => total 5.
12th Sep 2023, 11:43 AM
Lothar
Lothar - avatar
+ 4
Pranay PRASHANT NAIK , your `answer` has no relation to this post, and can be considered as spam.
14th Sep 2023, 6:22 AM
Lothar
Lothar - avatar
+ 3
Lother, thanks.
12th Sep 2023, 11:56 AM
Turles
Turles - avatar