@@ -17,11 +17,27 @@ def compute_transform_tables(
17
17
delete_cost : int ,
18
18
insert_cost : int ,
19
19
) -> 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
+ """
20
37
source_seq = list (source_string )
21
38
destination_seq = list (destination_string )
22
39
len_source_seq = len (source_seq )
23
40
len_destination_seq = len (destination_seq )
24
-
25
41
costs = [
26
42
[0 for _ in range (len_destination_seq + 1 )] for _ in range (len_source_seq + 1 )
27
43
]
@@ -31,33 +47,51 @@ def compute_transform_tables(
31
47
32
48
for i in range (1 , len_source_seq + 1 ):
33
49
costs [i ][0 ] = i * delete_cost
34
- ops [i ][0 ] = f"D{ source_seq [i - 1 ]:c } "
50
+ ops [i ][0 ] = f"D{ source_seq [i - 1 ]} "
35
51
36
52
for i in range (1 , len_destination_seq + 1 ):
37
53
costs [0 ][i ] = i * insert_cost
38
- ops [0 ][i ] = f"I{ destination_seq [i - 1 ]:c } "
54
+ ops [0 ][i ] = f"I{ destination_seq [i - 1 ]} "
39
55
40
56
for i in range (1 , len_source_seq + 1 ):
41
57
for j in range (1 , len_destination_seq + 1 ):
42
58
if source_seq [i - 1 ] == destination_seq [j - 1 ]:
43
59
costs [i ][j ] = costs [i - 1 ][j - 1 ] + copy_cost
44
- ops [i ][j ] = f"C{ source_seq [i - 1 ]:c } "
60
+ ops [i ][j ] = f"C{ source_seq [i - 1 ]} "
45
61
else :
46
62
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 ])
63
+ ops [i ][j ] = f"R{ source_seq [i - 1 ]} " + str (destination_seq [j - 1 ])
48
64
49
65
if costs [i - 1 ][j ] + delete_cost < costs [i ][j ]:
50
66
costs [i ][j ] = costs [i - 1 ][j ] + delete_cost
51
- ops [i ][j ] = f"D{ source_seq [i - 1 ]:c } "
67
+ ops [i ][j ] = f"D{ source_seq [i - 1 ]} "
52
68
53
69
if costs [i ][j - 1 ] + insert_cost < costs [i ][j ]:
54
70
costs [i ][j ] = costs [i ][j - 1 ] + insert_cost
55
- ops [i ][j ] = f"I{ destination_seq [j - 1 ]:c } "
71
+ ops [i ][j ] = f"I{ destination_seq [j - 1 ]} "
56
72
57
73
return costs , ops
58
74
59
75
60
76
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
+ """
61
95
if i == 0 and j == 0 :
62
96
return []
63
97
elif ops [i ][j ][0 ] in {"C" , "R" }:
0 commit comments