Skip to content

writing test for min-cost-string-conversion #3967

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 30 additions & 9 deletions strings/min_cost_string_conversion.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from tempfile import TemporaryFile
from typing import List, Tuple

"""
Expand Down Expand Up @@ -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))

Expand All @@ -97,35 +112,41 @@ 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]

file.write("%-16s" % ("Replace %c" % op[1] + " with " + str(op[2])))
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])

file.write("%-16s" % "Insert %c" % op[1])
file.write("\t\t\t" + "".join(string))
file.write("\r\n")

cost += 2
cost += insert_cost

i += 1

print("".join(string))
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)
36 changes: 36 additions & 0 deletions strings/tests/test_min_cost_string_conversion.py
Original file line number Diff line number Diff line change
@@ -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)