@@ -17,6 +17,15 @@ 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 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
+ """
20
29
source_seq = list (source_string )
21
30
destination_seq = list (destination_string )
22
31
len_source_seq = len (source_seq )
@@ -29,35 +38,51 @@ def compute_transform_tables(
29
38
["0" for _ in range (len_destination_seq + 1 )] for _ in range (len_source_seq + 1 )
30
39
]
31
40
41
+ # Removed ':c' specifier as it is generally used for integers to convert to a Unicode character not strings.
32
42
for i in range (1 , len_source_seq + 1 ):
33
43
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 ]} "
35
45
36
46
for i in range (1 , len_destination_seq + 1 ):
37
47
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 ]} "
39
49
40
50
for i in range (1 , len_source_seq + 1 ):
41
51
for j in range (1 , len_destination_seq + 1 ):
42
52
if source_seq [i - 1 ] == destination_seq [j - 1 ]:
43
53
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 ]} "
45
55
else :
46
56
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 ])
48
58
49
59
if costs [i - 1 ][j ] + delete_cost < costs [i ][j ]:
50
60
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 ]} "
52
62
53
63
if costs [i ][j - 1 ] + insert_cost < costs [i ][j ]:
54
64
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 ]} "
56
66
57
67
return costs , ops
58
68
59
69
60
70
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
+ """
61
86
if i == 0 and j == 0 :
62
87
return []
63
88
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]:
125
150
print ("" .join (string ))
126
151
print ("Cost: " , cost )
127
152
128
- file .write ("\r \n Minimum cost: " + str (cost ))
153
+ file .write ("\r \n Minimum cost: " + str (cost ))
0 commit comments