diff --git a/strings/min_cost_string_conversion.py b/strings/min_cost_string_conversion.py index e990aaa2679b..da3d8bcb348c 100644 --- a/strings/min_cost_string_conversion.py +++ b/strings/min_cost_string_conversion.py @@ -1,3 +1,4 @@ +from tempfile import TemporaryFile from typing import List, Tuple """ @@ -77,18 +78,32 @@ def assemble_transformation(ops: List[str], i: int, j: int) -> List[str]: return seq -if __name__ == "__main__": - _, operations = compute_transform_tables("Python", "Algorithms", -1, 1, 2, 2) +def min_cost_string_conversion( + str1: str, + str2: str, + copy_cost: int, + replace_cost: int, + delete_cost: int, + insert_cost: int, +): + _, operations = compute_transform_tables( + str1, + str2, + copy_cost, + replace_cost, + delete_cost, + insert_cost, + ) m = len(operations) n = len(operations[0]) sequence = assemble_transformation(operations, m - 1, n - 1) - string = list("Python") + string = list(str1) i = 0 cost = 0 - with open("min_cost.txt", "w") as file: + with TemporaryFile("w") as file: for op in sequence: print("".join(string)) @@ -97,7 +112,7 @@ def assemble_transformation(ops: List[str], i: int, j: int) -> List[str]: file.write("\t\t\t" + "".join(string)) file.write("\r\n") - cost -= 1 + cost += copy_cost elif op[0] == "R": string[i] = op[2] @@ -105,15 +120,15 @@ def assemble_transformation(ops: List[str], i: int, j: int) -> List[str]: file.write("\t\t" + "".join(string)) file.write("\r\n") - cost += 1 + cost += replace_cost elif op[0] == "D": - string.pop(i) + string.insert(i, "") file.write("%-16s" % "Delete %c" % op[1]) file.write("\t\t\t" + "".join(string)) file.write("\r\n") - cost += 2 + cost += delete_cost else: string.insert(i, op[1]) @@ -121,7 +136,7 @@ def assemble_transformation(ops: List[str], i: int, j: int) -> List[str]: file.write("\t\t\t" + "".join(string)) file.write("\r\n") - cost += 2 + cost += insert_cost i += 1 @@ -129,3 +144,9 @@ def assemble_transformation(ops: List[str], i: int, j: int) -> List[str]: print("Cost: ", cost) file.write("\r\nMinimum cost: " + str(cost)) + + return cost + + +if __name__ == "__main__": + min_cost_string_conversion("Python", "Algorithms", -1, 1, 2, 2) diff --git a/strings/tests/test_min_cost_string_conversion.py b/strings/tests/test_min_cost_string_conversion.py new file mode 100644 index 000000000000..52f432361131 --- /dev/null +++ b/strings/tests/test_min_cost_string_conversion.py @@ -0,0 +1,36 @@ +from strings.min_cost_string_conversion import min_cost_string_conversion + + +def test_calculate_min_cost_conversion_correct_answer_empty_string(): + s1, s2 = "", "short string" + copy, replace, delete, insert = 1, 2, 3, 4 + min_cost = min_cost_string_conversion(s1, s2, copy, replace, delete, insert) + assert min_cost == len(s2) * insert + + +def test_calculate_min_cost_conversion_correct_answer_first_correct_case(): + s1, s2 = "geek", "gesek" + copy, replace, delete, insert = 2, 4, 3, 8 + min_cost = min_cost_string_conversion(s1, s2, copy, replace, delete, insert) + assert min_cost == copy * 4 + insert + + +def test_calculate_min_cost_conversion_correct_answer_second_correct_case(): + s1, s2 = "cat", "cut" + copy, replace, delete, insert = 2, 6, 3, 8 + min_cost = min_cost_string_conversion(s1, s2, copy, replace, delete, insert) + assert min_cost == copy * 2 + replace + + +def test_calculate_min_cost_conversion_correct_answer_third_correct_case(): + s1, s2 = "sunday", "saturday" + copy, replace, delete, insert = 10, 20, 30, 40 + min_cost = min_cost_string_conversion(s1, s2, copy, replace, delete, insert) + assert min_cost == copy * 5 + insert * 2 + replace + + +def test_calculate_min_cost_conversion_correct_answer_fourth_correct_case(): + s1, s2 = "pythonista", "" + copy, replace, delete, insert = 4, 1, 3, 4 + min_cost = min_cost_string_conversion(s1, s2, copy, replace, delete, insert) + assert min_cost == delete * len(s1)