Skip to content

Commit 68d08f8

Browse files
committed
docs: add doctests for min_cost_string_conversion
1 parent 2b3d13f commit 68d08f8

1 file changed

Lines changed: 1 addition & 35 deletions

File tree

strings/min_cost_string_conversion.py

Lines changed: 1 addition & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,6 @@ def min_cost_string_conversion(str1: str, str2: str) -> int:
3131
0
3232
>>> min_cost_string_conversion("kitten", "sitting")
3333
3
34-
>>> min_cost_string_conversion("flaw", "lawn")
35-
2
3634
"""
3735
costs, _ = compute_transform_tables(
3836
source_string=str1,
@@ -56,19 +54,6 @@ def compute_transform_tables(
5654
"""
5755
Finds the most cost efficient sequence for converting one string into another
5856
using dynamic programming with specified costs.
59-
60-
Doctests:
61-
>>> costs, operations = compute_transform_tables("cat", "cut", 1, 2, 3, 3)
62-
>>> costs[0][:4]
63-
[0, 3, 6, 9]
64-
>>> costs[2][:4]
65-
[6, 4, 3, 6]
66-
>>> operations[0][:4]
67-
['0', 'Ic', 'Iu', 'It']
68-
>>> operations[3][:4]
69-
['Dt', 'Dt', 'Rtu', 'Ct']
70-
>>> compute_transform_tables("", "", 1, 2, 3, 3)
71-
([[0]], [['0']])
7257
"""
7358
len_source_seq = len(source_string)
7459
len_destination_seq = len(destination_string)
@@ -90,20 +75,17 @@ def compute_transform_tables(
9075

9176
for i in range(1, len_source_seq + 1):
9277
for j in range(1, len_destination_seq + 1):
93-
# Cost of copying or replacing the current character
9478
if source_string[i - 1] == destination_string[j - 1]:
9579
costs[i][j] = costs[i - 1][j - 1] + copy_cost
9680
ops[i][j] = f"C{source_string[i - 1]}"
9781
else:
9882
costs[i][j] = costs[i - 1][j - 1] + replace_cost
9983
ops[i][j] = f"R{source_string[i - 1]}{destination_string[j - 1]}"
10084

101-
# Check if deleting from source is cheaper
10285
if costs[i - 1][j] + delete_cost < costs[i][j]:
10386
costs[i][j] = costs[i - 1][j] + delete_cost
10487
ops[i][j] = f"D{source_string[i - 1]}"
10588

106-
# Check if inserting into destination is cheaper
10789
if costs[i][j - 1] + insert_cost < costs[i][j]:
10890
costs[i][j] = costs[i][j - 1] + insert_cost
10991
ops[i][j] = f"I{destination_string[j - 1]}"
@@ -114,21 +96,6 @@ def compute_transform_tables(
11496
def assemble_transformation(ops: list[list[str]], i: int, j: int) -> list[str]:
11597
"""
11698
Assembles the list of transformations based on the ops table.
117-
118-
Doctests:
119-
>>> ops = [['0', 'Ic', 'Iu', 'It'],
120-
... ['Dc', 'Cc', 'Iu', 'It'],
121-
... ['Da', 'Da', 'Rau', 'Rat'],
122-
... ['Dt', 'Dt', 'Rtu', 'Ct']]
123-
>>> x = len(ops) - 1
124-
>>> y = len(ops[0]) - 1
125-
>>> assemble_transformation(ops, x, y)
126-
['Cc', 'Rau', 'Ct']
127-
>>> ops1 = [['0']]
128-
>>> x1 = len(ops1) - 1
129-
>>> y1 = len(ops1[0]) - 1
130-
>>> assemble_transformation(ops1, x1, y1)
131-
[]
13299
"""
133100
if i == 0 and j == 0:
134101
return []
@@ -144,5 +111,4 @@ def assemble_transformation(ops: list[list[str]], i: int, j: int) -> list[str]:
144111

145112

146113
if __name__ == "__main__":
147-
# Run the doctests in this file
148-
doctest.testmod()
114+
doctest.testmod()

0 commit comments

Comments
 (0)