Skip to content

Commit 59ff87d

Browse files
Lonercodepre-commit-ci[bot]tianyizheng02
authored
Added doctests to min_cost_string_conversion.py and removed :c specifier (#11721)
* Added doctests to min_cost_string_conversion.py and removed :c specifier * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * resolved line length issues based on ruff requirements * modified in compliance with ruff for line length * Update strings/min_cost_string_conversion.py --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Tianyi Zheng <[email protected]>
1 parent 917ad62 commit 59ff87d

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed

Diff for: strings/min_cost_string_conversion.py

+35
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,23 @@ 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
22+
for converting one string into another.
23+
24+
>>> costs, operations = compute_transform_tables("cat", "cut", 1, 2, 3, 3)
25+
>>> costs[0][:4]
26+
[0, 3, 6, 9]
27+
>>> costs[2][:4]
28+
[6, 4, 3, 6]
29+
>>> operations[0][:4]
30+
['0', 'Ic', 'Iu', 'It']
31+
>>> operations[3][:4]
32+
['Dt', 'Dt', 'Rtu', 'Ct']
33+
34+
>>> compute_transform_tables("", "", 1, 2, 3, 3)
35+
([[0]], [['0']])
36+
"""
2037
source_seq = list(source_string)
2138
destination_seq = list(destination_string)
2239
len_source_seq = len(source_seq)
@@ -57,6 +74,24 @@ def compute_transform_tables(
5774

5875

5976
def assemble_transformation(ops: list[list[str]], i: int, j: int) -> list[str]:
77+
"""
78+
Assembles the transformations based on the ops table.
79+
80+
>>> ops = [['0', 'Ic', 'Iu', 'It'],
81+
... ['Dc', 'Cc', 'Iu', 'It'],
82+
... ['Da', 'Da', 'Rau', 'Rat'],
83+
... ['Dt', 'Dt', 'Rtu', 'Ct']]
84+
>>> x = len(ops) - 1
85+
>>> y = len(ops[0]) - 1
86+
>>> assemble_transformation(ops, x, y)
87+
['Cc', 'Rau', 'Ct']
88+
89+
>>> ops1 = [['0']]
90+
>>> x1 = len(ops1) - 1
91+
>>> y1 = len(ops1[0]) - 1
92+
>>> assemble_transformation(ops1, x1, y1)
93+
[]
94+
"""
6095
if i == 0 and j == 0:
6196
return []
6297
elif ops[i][j][0] in {"C", "R"}:

0 commit comments

Comments
 (0)