What's up with the "How Far?" Code Coach challenge? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

What's up with the "How Far?" Code Coach challenge?

Most of the Code Coach challenges I've done don't really have untold edge cases, so I'm not sure if I'm missing an edge case here. The objective for the "How Far?" medium challenge is basically to determine the amount of blocks between your home ("H") and the pond ("P") in a single string (Ex: An input of BBHBBBBPBBB should yield an output of 4 blocks). This was my first python code for it: print((lambda x: x.index('P') - x.index('H') - 1)(input()))) This worked for all test cases except #3 and #4, which are both hidden test cases. I've already tried putting the answer inside an abs() function in case the 'P' precedes the 'H'. I've also tried using the count method to count the blocks between the two, but nothing has worked so far.

4th May 2020, 5:05 PM
Zerokles
Zerokles - avatar
2 Answers
+ 2
Using the abs() function is a great idea, but you shouldn't put everything inside it. The following test cases will hopefully help you see the problem with your code. Input: BHPB Output: 0 Input: BPHB Output: 0
4th May 2020, 5:20 PM
Diego
Diego - avatar
+ 1
Diego Yeah, that was it. I shouldn’t have included the -1 in the abs() function. I’m getting rusty. Thanks.
4th May 2020, 5:33 PM
Zerokles
Zerokles - avatar