Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Longest Common Subsequence algorithm wrong? #225

Open
ghost opened this issue Jan 14, 2018 · 2 comments
Open

Longest Common Subsequence algorithm wrong? #225

ghost opened this issue Jan 14, 2018 · 2 comments

Comments

@ghost
Copy link

ghost commented Jan 14, 2018

Longest Common Subsequence algorithm solution doesn't work on.

Here is the test case from Tushar Roy's video on Longest Common Subsequence (https://www.youtube.com/watch?v=NnD96abizww).

class TestLongestCommonSubseq(object):
    def test_another(self):
        str_comp = StringCompare()
        str0 = 'ABCDAF'
        str1 = 'ACBCF'
        expected = 'ABCF'
        assert_equal(str_comp.longest_common_subseq(str0, str1), expected)

The solution says

     13                 if i == 0 or j == 0:
     14                     T[i][j] = 0
---> 15                 elif str0[j - 1] != str1[i - 1]:
     16                     T[i][j] = max(T[i][j - 1],
     17                                   T[i - 1][j])

IndexError: string index out of range
@WyattJia
Copy link

Can you put you full file?

@havanagrawal
Copy link

Notebooks

Bug
The variables num_rows and num_cols are defined as:

num_rows = len(str0) + 1
num_cols = len(str1) + 1

i and j iterate over num_rows and num_cols respectively, but the offending line checks str0[j - 1] and str1[i - 1], i.e. i and j are swapped.

The bug will manifest itself in cases where len(str0) != len(str1)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants