Skip to content

Commit 05f5983

Browse files
committed
Added doctests to min_cost_string_conversion.py and removed :c specifier
1 parent 00e9d86 commit 05f5983

File tree

1 file changed

+32
-7
lines changed

1 file changed

+32
-7
lines changed

Diff for: strings/min_cost_string_conversion.py

+32-7
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,15 @@ def compute_transform_tables(
1717
delete_cost: int,
1818
insert_cost: int,
1919
) -> tuple[list[list[int]], list[list[str]]]:
20+
"""
21+
Finds the most cost efficient sequence for converting one string into another.
22+
23+
>>> compute_transform_tables("cat", "cut", 1, 2, 3, 3)
24+
([[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']])
25+
26+
>>> compute_transform_tables("", "", 1, 2, 3, 3)
27+
([[0]], [['0']])
28+
"""
2029
source_seq = list(source_string)
2130
destination_seq = list(destination_string)
2231
len_source_seq = len(source_seq)
@@ -29,35 +38,51 @@ def compute_transform_tables(
2938
["0" for _ in range(len_destination_seq + 1)] for _ in range(len_source_seq + 1)
3039
]
3140

41+
# Removed ':c' specifier as it is generally used for integers to convert to a Unicode character not strings.
3242
for i in range(1, len_source_seq + 1):
3343
costs[i][0] = i * delete_cost
34-
ops[i][0] = f"D{source_seq[i - 1]:c}"
44+
ops[i][0] = f"D{source_seq[i - 1]}"
3545

3646
for i in range(1, len_destination_seq + 1):
3747
costs[0][i] = i * insert_cost
38-
ops[0][i] = f"I{destination_seq[i - 1]:c}"
48+
ops[0][i] = f"I{destination_seq[i - 1]}"
3949

4050
for i in range(1, len_source_seq + 1):
4151
for j in range(1, len_destination_seq + 1):
4252
if source_seq[i - 1] == destination_seq[j - 1]:
4353
costs[i][j] = costs[i - 1][j - 1] + copy_cost
44-
ops[i][j] = f"C{source_seq[i - 1]:c}"
54+
ops[i][j] = f"C{source_seq[i - 1]}"
4555
else:
4656
costs[i][j] = costs[i - 1][j - 1] + replace_cost
47-
ops[i][j] = f"R{source_seq[i - 1]:c}" + str(destination_seq[j - 1])
57+
ops[i][j] = f"R{source_seq[i - 1]}" + str(destination_seq[j - 1])
4858

4959
if costs[i - 1][j] + delete_cost < costs[i][j]:
5060
costs[i][j] = costs[i - 1][j] + delete_cost
51-
ops[i][j] = f"D{source_seq[i - 1]:c}"
61+
ops[i][j] = f"D{source_seq[i - 1]}"
5262

5363
if costs[i][j - 1] + insert_cost < costs[i][j]:
5464
costs[i][j] = costs[i][j - 1] + insert_cost
55-
ops[i][j] = f"I{destination_seq[j - 1]:c}"
65+
ops[i][j] = f"I{destination_seq[j - 1]}"
5666

5767
return costs, ops
5868

5969

6070
def assemble_transformation(ops: list[list[str]], i: int, j: int) -> list[str]:
71+
"""
72+
Assembles the transformations based on the information in the ops table.
73+
74+
>>> ops = [['0', 'Ic', 'Iu', 'It'], ['Dc', 'Cc', 'Iu', 'It'], ['Da', 'Da', 'Rau', 'Rat'], ['Dt', 'Dt', 'Rtu', 'Ct']]
75+
>>> x = len(ops) - 1
76+
>>> y = len(ops[0]) - 1
77+
>>> assemble_transformation(ops, x, y)
78+
['Cc', 'Rau', 'Ct']
79+
80+
>>> ops1 = [['0']]
81+
>>> x1 = len(ops1) - 1
82+
>>> y1 = len(ops1[0]) - 1
83+
>>> assemble_transformation(ops1, x1, y1)
84+
[]
85+
"""
6186
if i == 0 and j == 0:
6287
return []
6388
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]:
125150
print("".join(string))
126151
print("Cost: ", cost)
127152

128-
file.write("\r\nMinimum cost: " + str(cost))
153+
file.write("\r\nMinimum cost: " + str(cost))

0 commit comments

Comments
 (0)