Haseeb Khan
Haseeb Khan

Reputation: 1

Longest common subsequences, error when running code

`Here are the code and the error. I am not sure what to do for the declare any desired classes.

Traceback (most recent call last):
File "/home/runner/local/submission/main.py", line 149, in \<module\>
if test_case.execute(test_feedback):
File "/home/runner/local/submission/LCSTestCase.py", line 33, in execute
actual = user_matrix.get_longest_common_subsequences()
File "/home/runner/local/submission/LCSMatrix.py", line 60, in get_longest_common_subsequences
sequences = backtrack(self.row_count, self.column_count)
File "/home/runner/local/submission/LCSMatrix.py", line 49, in backtrack
elif str1\[i - 1\] == str2\[j - 1\]:
IndexError: string index out of range

init method: Initializes the LCSMatrix class with two input strings (str1 and str2).

It sets row_count and column_count attributes based on the lengths of the input strings.

Initializes a matrix (self.matrix) with dimensions (row_count + 1) x (column_count + 1) to store intermediate values for the LCS algorithm.

get_column_count method:

Returns the number of columns in the matrix, which is equivalent to the length of str2.

get_entry method:

Returns the value at the specified row_index and column_index in the matrix.

To be filled in with logic related to accessing the matrix elements.

get_row_count method:

Returns the number of rows in the matrix, which is equivalent to the length of str1.

get_longest_common_subsequences method:

This method aims to find the longest common subsequences between the two strings.

To be filled in with logic for finding LCS using the initialized matrix.

`# Your code here

Declare any desired classes to be used by LCSMatrix

class LCSMatrix:
    def __init__(self, str1, str2):
        self.row_count = len(str1)
        self.column_count = len(str2)

        self.matrix = [[0] * (self.column_count + 1) for _ in range(self.row_count + 1)]
    
    # Populate the matrix using dynamic programming
        for i in range(1, self.row_count + 1):
            for j in range(1, self.column_count + 1):
                if str1[i - 1] == str2[j - 1]:
                    self.matrix[i][j] = self.matrix[i - 1][j - 1] + 1
                else:
                    self.matrix[i][j] = max(self.matrix[i - 1][j], self.matrix[i][j - 1])
                
                
# Returns the number of columns in the matrix, which also equals the length
# of the second string passed to the constructor.
    def get_column_count(self):
        return self.column_count

# Returns the matrix entry at the specified row and column indices, or 0 if
# either index is out of bounds.

    def get_entry(self, row_index, column_index):
    
        if 0 <= row_index <= self.row_count and 0 <= column_index <= self.column_count:
            matrix_made = self.matrix[row_index][column_index]
            return matrix_made
        else:
           return 0

# Returns the number of rows in the matrix, which also equals the length
# of the first string passed to the constructor.
     def get_row_count(self):
        return self.row_count

# Returns the set of distinct, longest common subsequences between the two
# strings that were passed to the constructor.
     def get_longest_common_subsequences(self):
    
        def backtrack(i, j):
            if i == 0 or j == 0:
                return {""}
            elif str1[i - 1] == str2[j - 1]:
                return {subseq + str1[i - 1] for subseq in backtrack(i - 1, j - 1)}
           else:
            sequences = set()
            if self.matrix[i - 1][j] >= self.matrix[i][j - 1]:
                sequences |= backtrack(i - 1, j)
            if self.matrix[i][j - 1] >= self.matrix[i - 1][j]:
                sequences |= backtrack(i, j - 1)
            return sequences

    str1 = str2 = ""
    sequences = backtrack(self.row_count, self.column_count)
    return sequences`

Upvotes: 0

Views: 74

Answers (1)

gru.ai
gru.ai

Reputation: 11

Not sure if the following code works?

class LCSMatrix:
    def __init__(self, str1, str2):
        self.str1 = str1
        self.str2 = str2
        self.row_count = len(str1)
        self.column_count = len(str2)
        self.matrix = [[0] * (self.column_count + 1) for _ in range(self.row_count + 1)]
        for i in range(1, self.row_count + 1):
            for j in range(1, self.column_count + 1):
                if str1[i - 1] == str2[j - 1]:
                    self.matrix[i][j] = self.matrix[i - 1][j - 1] + 1
                else:
                    self.matrix[i][j] = max(self.matrix[i - 1][j], self.matrix[i][j - 1])

    def get_longest_common_subsequences(self):
        def backtrack(i, j):
            if i == 0 or j == 0:
                return {""}
            elif self.str1[i - 1] == self.str2[j - 1]:
                return {subseq + self.str1[i - 1] for subseq in backtrack(i - 1, j - 1)}
            else:
                sequences = set()
                if self.matrix[i - 1][j] >= self.matrix[i][j - 1]:
                    sequences |= backtrack(i - 1, j)
                if self.matrix[i][j - 1] >= self.matrix[i - 1][j]:
                    sequences |= backtrack(i, j - 1)
                return sequences

        return backtrack(self.row_count, self.column_count)

# Test the LCSMatrix with example strings
lcs_matrix = LCSMatrix("abcde", "ace")
print(lcs_matrix.get_longest_common_subsequences())

Upvotes: 0

Related Questions