From 5d068870bf5ca10bfada3a409dce67005968ae8b Mon Sep 17 00:00:00 2001 From: spainn <148598154+spainn@users.noreply.github.com> Date: Fri, 22 Mar 2024 15:02:03 -0500 Subject: [PATCH] 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> --- .../python/02_longest_common_substring.py | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/11_dynamic_programming/python/02_longest_common_substring.py b/11_dynamic_programming/python/02_longest_common_substring.py index 7cf05f4..1e879a5 100644 --- a/11_dynamic_programming/python/02_longest_common_substring.py +++ b/11_dynamic_programming/python/02_longest_common_substring.py @@ -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)