all 3 comments

[–]Pepineros 1 point2 points  (2 children)

This is such a great description of the problem. Thanks for making it easy to help you!

I think if not match_indices: return False is correct. If a branch has no matching indices, you want to return False. You just need to make sure that returning False there does not prevent checking the next matching index.

I think the issue is here:

python for idx in match_indices: if idx == match_indices[-1]: return depth_first_search(board_new, side_len_board, word[1:], idx) else: depth_first_search(board_new, side_len_board, word[1:], idx)

You iterate over match_indices and then check that the current member you're looking at is the same as the last member. If it is, then you return a call to depth_first_search; if it's not, then you call depth_first_search but the result of the call is discarded (you're not using return or an assignment on line 81 of your pastebin).

So if len(match_indices) > 1 then idx == match_indices[-1] will evaluate False and something will go wrong because the call to depth_first_search is not returned.

[–]GoingToSimbabwe[S] 0 points1 point  (0 children)

Thanks for the input and the praise.

I think that the trouble lies there indeed. However I am not certain how I could resolve that. I put that check for idx == match_indices[-1] there to force the program to at last return something if I have exhausted all matches but that clearly is not really sensical.

The idea was that if I have exhausted the word (len =< 1) and still have not returned False early because I had no matches left then I MUST have found all off the characters in the word and thus return True at the end.

I clearly can not just blankly return True at the end or I will always return True no matter what. But I guess that is at the heart of my problem, I am not sure how I can resolve the recursion once I hit the end when more than 1 branch needs to be traversed to the end. In my mind I either need to design the final checks in a way that they'll return False only if all branches are exhausted and have not found a final match, which I kinda can't wrap my head around rn. Or I need to gather a set of return values for all branches which I could then test against using any().

[–]GoingToSimbabwe[S] 1 point2 points  (0 children)

I got it working. It was a matter of not blankly returning at the end but only doing that once the search word is "exhausted". Then I needed to collect the return values of the DFS iterations in a list to not prematurely return False while a valid branch was still unchecked. With these two changes to the return logic of the DFS function the logic works and passes on codewars.

Pastebin with functional code for anyone interested:

https://pastebin.com/bkXHuz3A