Skip to content
109 changes: 71 additions & 38 deletions strings/min_cost_string_conversion.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,34 @@ def compute_transform_tables(
delete_cost: int,
insert_cost: int,
) -> tuple[list[list[int]], list[list[str]]]:
"""
Compute transformation tables for string alignment.

Args:
source_string (str): The source string.
destination_string (str): The destination string.
copy_cost (int): Cost of copying a character.
replace_cost (int): Cost of replacing a character.
delete_cost (int): Cost of deleting a character.
insert_cost (int): Cost of inserting a character.

Returns:
tuple[list[list[int]], list[list[str]]]: A tuple containing two lists:
- The first list is a table of minimum costs for transformations.
- The second list is a table of transformation operations.

Example:
>>> costs, ops = compute_transform_tables("Python", "Algorithms", -1, 1, 2, 2)
>>> len(costs)
7
>>> len(ops)
7
>>> len(costs[0])
11
>>> len(ops[0])
11
"""

source_seq = list(source_string)
destination_seq = list(destination_string)
len_source_seq = len(source_seq)
Expand Down Expand Up @@ -58,6 +86,26 @@ def compute_transform_tables(


def assemble_transformation(ops: list[list[str]], i: int, j: int) -> list[str]:
"""
Assemble a list of transformation operations.

Args:
ops (list[list[str]]): The table of transformation operations.
i (int): The current row.
j (int): The current column.

Returns:
list[str]: A list of transformation operations.

Example:
>>> ops = [["0", "0", "0"], ["0", "Ct", "Dh"], ["0", "Ie", "Ri"]]
>>> assemble_transformation(ops, 2, 2)
['Ri', 'Ie', 'Ct', 'Dh']
>>> assemble_transformation(ops, 1, 1)
['Ct', 'Dh']
>>> assemble_transformation(ops, 0, 0)
[]
"""
if i == 0 and j == 0:
return []
else:
Expand Down Expand Up @@ -86,44 +134,29 @@ def assemble_transformation(ops: list[list[str]], i: int, j: int) -> list[str]:
i = 0
cost = 0

with open("min_cost.txt", "w") as file:
for op in sequence:
print("".join(string))

if op[0] == "C":
file.write("%-16s" % "Copy %c" % op[1])
file.write("\t\t\t" + "".join(string))
file.write("\r\n")

cost -= 1
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
elif op[0] == "D":
string.pop(i)

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

cost += 2
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
for op in sequence:
print("".join(string))
if op[0] == "C":
print("%-16s" % "Copy %c" % op[1])
cost -= 1
elif op[0] == "R":
string[i] = op[2]
print("%-16s" % ("Replace %c" % op[1] + " with " + str(op[2])))
cost += 1
elif op[0] == "D":
string.pop(i)
print("%-16s" % "Delete %c" % op[1])
cost += 2
else:
string.insert(i, op[1])
print("%-16s" % "Insert %c" % op[1])
cost += 2

i += 1
i += 1

print("".join(string))
print("Cost: ", cost)
print("".join(string))
print("Cost: ", cost)
print("Minimum cost: " + str(cost))
import doctest

file.write("\r\nMinimum cost: " + str(cost))
doctest.testmod()