+ 1

Still failing some test cases

You are walking from your house to a pond that is down your street. How many blocks over will you have to walk until you get to the pond? Task: Evaluate how many blocks you will have to walk if you are given a representation of your street where H represents your house, P represents the pond, and every B represents a block in between the two. Input Format: A string of letters representing your house, the pond, and blocks on your street. Output Format: An integer value that represents the number of blocks between your house and the pond. Sample Input: BBHBBBBPBBB Sample Output: 4 My_code address = input() house =address.index('H') pond = address.index('P') blocks = address[house:pond+1] print(blocks.count('B'))

16th May 2025, 3:52 PM
Victor Uwemedimo George
3 Answers
+ 3
Victor Uwemedimo George Your code fails for the case when index of H is greater than index of P. Instead you can try:- `` blocks = address[house:pond+1] if house < pond else address[pond:house+1] ``
16th May 2025, 3:59 PM
Gulshan Mahawar
Gulshan Mahawar - avatar
+ 1
I found the problem already , because the pond could be before the house and slicing the string would result in an empty string, so i used an if statement to switch between address[house:pond] and address[pond: house]
16th May 2025, 4:02 PM
Victor Uwemedimo George
+ 1
thanks gulshan
16th May 2025, 4:03 PM
Victor Uwemedimo George