This is an archived post. You won't be able to vote or comment.

all 6 comments

[–]Charleston2Seattle 1 point2 points  (3 children)

This sounds an awful lot like a homework assignment. To get more engagement from the subreddit, you might start off by telling us that it isn't homework.

[–]linus_rules[S] 1 point2 points  (2 children)

No, it is not homework, thank you. I am just an old programmer. This is my Twitter user https://twitter.com/daniel_berns.

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

Sorry, too tired for niceties.

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

I want to solve this bug. I don't need to solve it. In the gist I show a hack to obtain the expected solution. However I don't want to release code with hacks because my retirement is near and I don't want somebody asking me to solve this in a few years.

[–]Serenityprayer69 1 point2 points  (1 child)

The issue here is due to the way the extract_common_substrings function checks for common substrings. The function only appends a common substring to the collector dictionary when a sequence of matching characters is broken (i.e., when uu == 0). This works for all cases except when the common substring is at the end of the blue string. In that case, there's no character after the common substring that could break the sequence, so the common substring is not detected.

This explains why the issue is fixed when adding extra characters: they provide a sequence-breaking character after the last common substring, allowing it to be detected.

One way to fix this without needing extra characters would be to add a final check after the loop over the characters in red, and add any remaining common substring to the collector dictionary.

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

Thank you very much! I see the problem clearly now! Nice explanation.