Skip to content

Commit 6abab54

Browse files
Minimum cost for transformation from one string to another using basic operations
1 parent a753acf commit 6abab54

File tree

1 file changed

+7
-8
lines changed

1 file changed

+7
-8
lines changed

strings/min-cost-string-conversion.py

+7-8
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
except NameError:
66
xrange = range #Python 3
77

8-
def compute_transform_tables(X, Y, cC, cR, cD, cI):
98
'''
109
Algorithm for calculating the most cost-efficient sequence for converting one string into another.
1110
The only allowed operations are
@@ -14,6 +13,7 @@ def compute_transform_tables(X, Y, cC, cR, cD, cI):
1413
---Delete character with cost cD
1514
---Insert character with cost cI
1615
'''
16+
def compute_transform_tables(X, Y, cC, cR, cD, cI):
1717
X = list(X)
1818
Y = list(Y)
1919
m = len(X)
@@ -81,13 +81,13 @@ def assemble_transformation(ops, i, j):
8181
i = 0
8282
cost = 0
8383
for op in sequence:
84-
print ''.join(string)
84+
print(''.join(string))
85+
8586
if op[0] == 'C':
8687
file.write('%-16s' % 'Copy %c' % op[1])
8788
file.write('\t\t\t' + ''.join(string))
8889
file.write('\r\n')
8990

90-
i += 1
9191
cost -= 1
9292
elif op[0] == 'R':
9393
string[i] = op[2]
@@ -96,7 +96,6 @@ def assemble_transformation(ops, i, j):
9696
file.write('\t\t' + ''.join(string))
9797
file.write('\r\n')
9898

99-
i += 1
10099
cost += 1
101100
elif op[0] == 'D':
102101
string.pop(i)
@@ -105,7 +104,6 @@ def assemble_transformation(ops, i, j):
105104
file.write('\t\t\t' + ''.join(string))
106105
file.write('\r\n')
107106

108-
i += 1
109107
cost += 2
110108
else:
111109
string.insert(i, op[1])
@@ -114,11 +112,12 @@ def assemble_transformation(ops, i, j):
114112
file.write('\t\t\t' + ''.join(string))
115113
file.write('\r\n')
116114

117-
i += 1
118115
cost += 2
119116

120-
print ''.join(string)
121-
print 'Cost: ', cost
117+
i += 1
118+
119+
print(''.join(string))
120+
print('Cost: ', cost)
122121

123122
file.write('\r\nMinimum cost: ' + str(cost))
124123
file.close()

0 commit comments

Comments
 (0)