5
5
except NameError :
6
6
xrange = range #Python 3
7
7
8
- def compute_transform_tables (X , Y , cC , cR , cD , cI ):
9
8
'''
10
9
Algorithm for calculating the most cost-efficient sequence for converting one string into another.
11
10
The only allowed operations are
@@ -14,6 +13,7 @@ def compute_transform_tables(X, Y, cC, cR, cD, cI):
14
13
---Delete character with cost cD
15
14
---Insert character with cost cI
16
15
'''
16
+ def compute_transform_tables (X , Y , cC , cR , cD , cI ):
17
17
X = list (X )
18
18
Y = list (Y )
19
19
m = len (X )
@@ -81,13 +81,13 @@ def assemble_transformation(ops, i, j):
81
81
i = 0
82
82
cost = 0
83
83
for op in sequence :
84
- print '' .join (string )
84
+ print ('' .join (string ))
85
+
85
86
if op [0 ] == 'C' :
86
87
file .write ('%-16s' % 'Copy %c' % op [1 ])
87
88
file .write ('\t \t \t ' + '' .join (string ))
88
89
file .write ('\r \n ' )
89
90
90
- i += 1
91
91
cost -= 1
92
92
elif op [0 ] == 'R' :
93
93
string [i ] = op [2 ]
@@ -96,7 +96,6 @@ def assemble_transformation(ops, i, j):
96
96
file .write ('\t \t ' + '' .join (string ))
97
97
file .write ('\r \n ' )
98
98
99
- i += 1
100
99
cost += 1
101
100
elif op [0 ] == 'D' :
102
101
string .pop (i )
@@ -105,7 +104,6 @@ def assemble_transformation(ops, i, j):
105
104
file .write ('\t \t \t ' + '' .join (string ))
106
105
file .write ('\r \n ' )
107
106
108
- i += 1
109
107
cost += 2
110
108
else :
111
109
string .insert (i , op [1 ])
@@ -114,11 +112,12 @@ def assemble_transformation(ops, i, j):
114
112
file .write ('\t \t \t ' + '' .join (string ))
115
113
file .write ('\r \n ' )
116
114
117
- i += 1
118
115
cost += 2
119
116
120
- print '' .join (string )
121
- print 'Cost: ' , cost
117
+ i += 1
118
+
119
+ print ('' .join (string ))
120
+ print ('Cost: ' , cost )
122
121
123
122
file .write ('\r \n Minimum cost: ' + str (cost ))
124
123
file .close ()
0 commit comments