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.
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
+ 1
Diego Yeah, that was it. I shouldnât have included the -1 in the abs() function. Iâm getting rusty.
Thanks.