From 05f5983348ca05f0f2b44e66c1636ea26c4e84f8 Mon Sep 17 00:00:00 2001 From: lonercode Date: Thu, 3 Oct 2024 21:41:12 +0100 Subject: [PATCH 1/5] Added doctests to min_cost_string_conversion.py and removed :c specifier --- strings/min_cost_string_conversion.py | 39 ++++++++++++++++++++++----- 1 file changed, 32 insertions(+), 7 deletions(-) diff --git a/strings/min_cost_string_conversion.py b/strings/min_cost_string_conversion.py index d147a9d7954c..6e8a036aed79 100644 --- a/strings/min_cost_string_conversion.py +++ b/strings/min_cost_string_conversion.py @@ -17,6 +17,15 @@ def compute_transform_tables( delete_cost: int, insert_cost: int, ) -> tuple[list[list[int]], list[list[str]]]: + """ + Finds the most cost efficient sequence for converting one string into another. + + >>> compute_transform_tables("cat", "cut", 1, 2, 3, 3) + ([[0, 3, 6, 9], [3, 1, 4, 7], [6, 4, 3, 6], [9, 7, 6, 4]], [['0', 'Ic', 'Iu', 'It'], ['Dc', 'Cc', 'Iu', 'It'], ['Da', 'Da', 'Rau', 'Rat'], ['Dt', 'Dt', 'Rtu', 'Ct']]) + + >>> compute_transform_tables("", "", 1, 2, 3, 3) + ([[0]], [['0']]) + """ source_seq = list(source_string) destination_seq = list(destination_string) len_source_seq = len(source_seq) @@ -29,35 +38,51 @@ def compute_transform_tables( ["0" for _ in range(len_destination_seq + 1)] for _ in range(len_source_seq + 1) ] + # Removed ':c' specifier as it is generally used for integers to convert to a Unicode character not strings. for i in range(1, len_source_seq + 1): costs[i][0] = i * delete_cost - ops[i][0] = f"D{source_seq[i - 1]:c}" + ops[i][0] = f"D{source_seq[i - 1]}" for i in range(1, len_destination_seq + 1): costs[0][i] = i * insert_cost - ops[0][i] = f"I{destination_seq[i - 1]:c}" + ops[0][i] = f"I{destination_seq[i - 1]}" for i in range(1, len_source_seq + 1): for j in range(1, len_destination_seq + 1): if source_seq[i - 1] == destination_seq[j - 1]: costs[i][j] = costs[i - 1][j - 1] + copy_cost - ops[i][j] = f"C{source_seq[i - 1]:c}" + ops[i][j] = f"C{source_seq[i - 1]}" else: costs[i][j] = costs[i - 1][j - 1] + replace_cost - ops[i][j] = f"R{source_seq[i - 1]:c}" + str(destination_seq[j - 1]) + ops[i][j] = f"R{source_seq[i - 1]}" + str(destination_seq[j - 1]) if costs[i - 1][j] + delete_cost < costs[i][j]: costs[i][j] = costs[i - 1][j] + delete_cost - ops[i][j] = f"D{source_seq[i - 1]:c}" + ops[i][j] = f"D{source_seq[i - 1]}" if costs[i][j - 1] + insert_cost < costs[i][j]: costs[i][j] = costs[i][j - 1] + insert_cost - ops[i][j] = f"I{destination_seq[j - 1]:c}" + ops[i][j] = f"I{destination_seq[j - 1]}" return costs, ops def assemble_transformation(ops: list[list[str]], i: int, j: int) -> list[str]: + """ + Assembles the transformations based on the information in the ops table. + + >>> ops = [['0', 'Ic', 'Iu', 'It'], ['Dc', 'Cc', 'Iu', 'It'], ['Da', 'Da', 'Rau', 'Rat'], ['Dt', 'Dt', 'Rtu', 'Ct']] + >>> x = len(ops) - 1 + >>> y = len(ops[0]) - 1 + >>> assemble_transformation(ops, x, y) + ['Cc', 'Rau', 'Ct'] + + >>> ops1 = [['0']] + >>> x1 = len(ops1) - 1 + >>> y1 = len(ops1[0]) - 1 + >>> assemble_transformation(ops1, x1, y1) + [] + """ if i == 0 and j == 0: return [] elif ops[i][j][0] in {"C", "R"}: @@ -125,4 +150,4 @@ def assemble_transformation(ops: list[list[str]], i: int, j: int) -> list[str]: print("".join(string)) print("Cost: ", cost) - file.write("\r\nMinimum cost: " + str(cost)) + file.write("\r\nMinimum cost: " + str(cost)) \ No newline at end of file From 16796d5127e667bc5e0c329ae6818ce50b2efbf4 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Thu, 3 Oct 2024 20:56:42 +0000 Subject: [PATCH 2/5] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- strings/min_cost_string_conversion.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/strings/min_cost_string_conversion.py b/strings/min_cost_string_conversion.py index 6e8a036aed79..2dbc35fd2497 100644 --- a/strings/min_cost_string_conversion.py +++ b/strings/min_cost_string_conversion.py @@ -70,7 +70,7 @@ def compute_transform_tables( def assemble_transformation(ops: list[list[str]], i: int, j: int) -> list[str]: """ Assembles the transformations based on the information in the ops table. - + >>> ops = [['0', 'Ic', 'Iu', 'It'], ['Dc', 'Cc', 'Iu', 'It'], ['Da', 'Da', 'Rau', 'Rat'], ['Dt', 'Dt', 'Rtu', 'Ct']] >>> x = len(ops) - 1 >>> y = len(ops[0]) - 1 @@ -150,4 +150,4 @@ def assemble_transformation(ops: list[list[str]], i: int, j: int) -> list[str]: print("".join(string)) print("Cost: ", cost) - file.write("\r\nMinimum cost: " + str(cost)) \ No newline at end of file + file.write("\r\nMinimum cost: " + str(cost)) From 69af1771cefacddf255a16b5eb70f308ba11c440 Mon Sep 17 00:00:00 2001 From: lonercode Date: Fri, 4 Oct 2024 08:04:44 +0100 Subject: [PATCH 3/5] resolved line length issues based on ruff requirements --- strings/min_cost_string_conversion.py | 31 +++++++++++++++++++-------- 1 file changed, 22 insertions(+), 9 deletions(-) diff --git a/strings/min_cost_string_conversion.py b/strings/min_cost_string_conversion.py index 6e8a036aed79..f9817699b5a0 100644 --- a/strings/min_cost_string_conversion.py +++ b/strings/min_cost_string_conversion.py @@ -18,10 +18,18 @@ def compute_transform_tables( insert_cost: int, ) -> tuple[list[list[int]], list[list[str]]]: """ - Finds the most cost efficient sequence for converting one string into another. - - >>> compute_transform_tables("cat", "cut", 1, 2, 3, 3) - ([[0, 3, 6, 9], [3, 1, 4, 7], [6, 4, 3, 6], [9, 7, 6, 4]], [['0', 'Ic', 'Iu', 'It'], ['Dc', 'Cc', 'Iu', 'It'], ['Da', 'Da', 'Rau', 'Rat'], ['Dt', 'Dt', 'Rtu', 'Ct']]) + Finds the most cost efficient sequence + for converting one string into another. + + >>> costs, operations = compute_transform_tables("cat", "cut", 1, 2, 3, 3) + >>> costs[0][:4] + [0, 3, 6, 9] + >>> costs[2][:4] + [6, 4, 3, 6] + >>> operations[0][:4] + ['0', 'Ic', 'Iu', 'It'] + >>> operations[3][:4] + ['Dt', 'Dt', 'Rtu', 'Ct'] >>> compute_transform_tables("", "", 1, 2, 3, 3) ([[0]], [['0']]) @@ -38,7 +46,9 @@ def compute_transform_tables( ["0" for _ in range(len_destination_seq + 1)] for _ in range(len_source_seq + 1) ] - # Removed ':c' specifier as it is generally used for integers to convert to a Unicode character not strings. + # Removed ':c' specifier as it is generally + # used for integers to convert to a Unicode + # character not strings. for i in range(1, len_source_seq + 1): costs[i][0] = i * delete_cost ops[i][0] = f"D{source_seq[i - 1]}" @@ -69,9 +79,12 @@ def compute_transform_tables( def assemble_transformation(ops: list[list[str]], i: int, j: int) -> list[str]: """ - Assembles the transformations based on the information in the ops table. - - >>> ops = [['0', 'Ic', 'Iu', 'It'], ['Dc', 'Cc', 'Iu', 'It'], ['Da', 'Da', 'Rau', 'Rat'], ['Dt', 'Dt', 'Rtu', 'Ct']] + Assembles the transformations based on the ops table. + + >>> ops = [['0', 'Ic', 'Iu', 'It'], + ... ['Dc', 'Cc', 'Iu', 'It'], + ... ['Da', 'Da', 'Rau', 'Rat'], + ... ['Dt', 'Dt', 'Rtu', 'Ct']] >>> x = len(ops) - 1 >>> y = len(ops[0]) - 1 >>> assemble_transformation(ops, x, y) @@ -150,4 +163,4 @@ def assemble_transformation(ops: list[list[str]], i: int, j: int) -> list[str]: print("".join(string)) print("Cost: ", cost) - file.write("\r\nMinimum cost: " + str(cost)) \ No newline at end of file + file.write("\r\nMinimum cost: " + str(cost)) From a53a0952828d74f0d2aa1baa8271111f3bf262f4 Mon Sep 17 00:00:00 2001 From: lonercode Date: Fri, 4 Oct 2024 08:17:57 +0100 Subject: [PATCH 4/5] modified in compliance with ruff for line length --- strings/min_cost_string_conversion.py | 3 --- 1 file changed, 3 deletions(-) diff --git a/strings/min_cost_string_conversion.py b/strings/min_cost_string_conversion.py index c54aecb3b6fd..f9817699b5a0 100644 --- a/strings/min_cost_string_conversion.py +++ b/strings/min_cost_string_conversion.py @@ -79,9 +79,6 @@ def compute_transform_tables( def assemble_transformation(ops: list[list[str]], i: int, j: int) -> list[str]: """ - Assembles the transformations based on the information in the ops table. - - >>> ops = [['0', 'Ic', 'Iu', 'It'], ['Dc', 'Cc', 'Iu', 'It'], ['Da', 'Da', 'Rau', 'Rat'], ['Dt', 'Dt', 'Rtu', 'Ct']] Assembles the transformations based on the ops table. >>> ops = [['0', 'Ic', 'Iu', 'It'], From fbcd20eb298e667eda6cb806b4f117e7055a242f Mon Sep 17 00:00:00 2001 From: Tianyi Zheng Date: Fri, 4 Oct 2024 02:33:27 -0700 Subject: [PATCH 5/5] Update strings/min_cost_string_conversion.py --- strings/min_cost_string_conversion.py | 3 --- 1 file changed, 3 deletions(-) diff --git a/strings/min_cost_string_conversion.py b/strings/min_cost_string_conversion.py index f9817699b5a0..7cf57017d9a1 100644 --- a/strings/min_cost_string_conversion.py +++ b/strings/min_cost_string_conversion.py @@ -46,9 +46,6 @@ def compute_transform_tables( ["0" for _ in range(len_destination_seq + 1)] for _ in range(len_source_seq + 1) ] - # Removed ':c' specifier as it is generally - # used for integers to convert to a Unicode - # character not strings. for i in range(1, len_source_seq + 1): costs[i][0] = i * delete_cost ops[i][0] = f"D{source_seq[i - 1]}"