0

How far? on Python

Hello everybody! I'm solving a problem called how far away? Where the following conditions are described : "You walk from your house to the pond that is on your street. How many blocks do you have to walk before you reach the pond? Task: Estimate how many blocks you will have to walk if you are given a representation of your street, where X represents your house, p represents a pond, and each block B represents in between. Input format: A string of letters representing your home, pond, and neighborhoods on your street. Output format: An integer value representing the number of blocks between your house and the pond. Entering a sample: BBHBBBBPBBB Example Output: 4 " my solution does not work for the test number 3 and 4. I can not understand what is the matter. here is my solution text = input (). upper () data = list (text [text. index ('H') + 1: text. index ('P')]) print (len (data))

18th Jun 2021, 3:07 PM
maks
7 Answers
+ 4
maks , a very simple way to do this task could be: ▪︎take input ▪︎get index of "H" ▪︎get index of "P" ▪︎subtract the both index values and make the result an absolute value, so that result is positive ▪︎subtract 1 from the absolute value happy coding!
18th Jun 2021, 4:09 PM
Lothar
Lothar - avatar
+ 2
Did you see the result for BBBBBBPBBBBH This is = 0. Is that true?
18th Jun 2021, 3:26 PM
JaScript
JaScript - avatar
+ 2
Maybe you find this thread helpful too https://www.sololearn.com/Discuss/2804097/?ref=app
18th Jun 2021, 4:13 PM
Lisa
Lisa - avatar
+ 2
Thanks for the tip Lothar ;)
18th Jun 2021, 4:45 PM
maks
+ 2
My solution od this task: letter = input() positionH =letter.index("H") positionP =letter.index("P") way = abs(positionP - positionH ) - 1 print(way)
22nd Feb 2022, 6:17 AM
Przemysław Komański
Przemysław Komański - avatar
+ 1
maks , one issue i detected is that it works correctly (showing 4) when having this input "BBHBBBBPBBB". by swapping "H" with "P" like this "BBPBBBBHBBB"it will show 0, but should be 4. try to check your code and fix it. happy coding and good duccess!
18th Jun 2021, 3:31 PM
Lothar
Lothar - avatar
0
Pond cant be located both ways
22nd Feb 2023, 11:37 AM
Evgeny Sergeev
Evgeny Sergeev - avatar