Update 02_longest_common_substring.py (#276)

The original code did not work correctly out of the box.  It generated a (4,5) grid instead of a (5,4) grid as the book's answer grid shows.  Also on line 10 of my code I changed `dp_table[i-1][i-1] + 1` to `dp_table[i-1][j-1] +1`.  I also updated the display to display by row.

Co-authored-by: WhileRested <148598154+WhileRested@users.noreply.github.com>
This commit is contained in:
spainn
2024-03-22 15:02:03 -05:00
committed by GitHub
parent 7d16656674
commit 5d068870bf

View File

@@ -1,13 +1,16 @@
dp_table_blue = ["b", "l", "u", "e"]
dp_table_clues = ["c", "l", "u", "e", "s"]
dp_table = [[0 for i in range(len(dp_table_blue))] for i in range(len(dp_table_clues))] # (5,4)
print(dp_table)
dp_table = [[0 for i in range(len(dp_table_clues))] for i in range(len(dp_table_blue))] # (5,4)
# for each row
for i in range(0, len(dp_table_blue)):
# for each col
for j in range(0, len(dp_table_clues)):
if dp_table_clues[j] == dp_table_blue[i]:
dp_table[i][j] = dp_table[i-1][i-1] + 1
dp_table[i][j] = dp_table[i-1][j-1] + 1
else:
dp_table[i][j] = 0
print(dp_table)
# display table
for i in dp_table:
print(i)