mirror of
https://github.com/TheAlgorithms/Python.git
synced 2025-02-22 09:12:08 +00:00
Minimum cost for transformation from one string to another using basic operations
This commit is contained in:
parent
a753acf1de
commit
6abab54c2b
|
@ -5,7 +5,6 @@ try:
|
||||||
except NameError:
|
except NameError:
|
||||||
xrange = range #Python 3
|
xrange = range #Python 3
|
||||||
|
|
||||||
def compute_transform_tables(X, Y, cC, cR, cD, cI):
|
|
||||||
'''
|
'''
|
||||||
Algorithm for calculating the most cost-efficient sequence for converting one string into another.
|
Algorithm for calculating the most cost-efficient sequence for converting one string into another.
|
||||||
The only allowed operations are
|
The only allowed operations are
|
||||||
|
@ -14,6 +13,7 @@ The only allowed operations are
|
||||||
---Delete character with cost cD
|
---Delete character with cost cD
|
||||||
---Insert character with cost cI
|
---Insert character with cost cI
|
||||||
'''
|
'''
|
||||||
|
def compute_transform_tables(X, Y, cC, cR, cD, cI):
|
||||||
X = list(X)
|
X = list(X)
|
||||||
Y = list(Y)
|
Y = list(Y)
|
||||||
m = len(X)
|
m = len(X)
|
||||||
|
@ -81,13 +81,13 @@ if __name__ == '__main__':
|
||||||
i = 0
|
i = 0
|
||||||
cost = 0
|
cost = 0
|
||||||
for op in sequence:
|
for op in sequence:
|
||||||
print ''.join(string)
|
print(''.join(string))
|
||||||
|
|
||||||
if op[0] == 'C':
|
if op[0] == 'C':
|
||||||
file.write('%-16s' % 'Copy %c' % op[1])
|
file.write('%-16s' % 'Copy %c' % op[1])
|
||||||
file.write('\t\t\t' + ''.join(string))
|
file.write('\t\t\t' + ''.join(string))
|
||||||
file.write('\r\n')
|
file.write('\r\n')
|
||||||
|
|
||||||
i += 1
|
|
||||||
cost -= 1
|
cost -= 1
|
||||||
elif op[0] == 'R':
|
elif op[0] == 'R':
|
||||||
string[i] = op[2]
|
string[i] = op[2]
|
||||||
|
@ -96,7 +96,6 @@ if __name__ == '__main__':
|
||||||
file.write('\t\t' + ''.join(string))
|
file.write('\t\t' + ''.join(string))
|
||||||
file.write('\r\n')
|
file.write('\r\n')
|
||||||
|
|
||||||
i += 1
|
|
||||||
cost += 1
|
cost += 1
|
||||||
elif op[0] == 'D':
|
elif op[0] == 'D':
|
||||||
string.pop(i)
|
string.pop(i)
|
||||||
|
@ -105,7 +104,6 @@ if __name__ == '__main__':
|
||||||
file.write('\t\t\t' + ''.join(string))
|
file.write('\t\t\t' + ''.join(string))
|
||||||
file.write('\r\n')
|
file.write('\r\n')
|
||||||
|
|
||||||
i += 1
|
|
||||||
cost += 2
|
cost += 2
|
||||||
else:
|
else:
|
||||||
string.insert(i, op[1])
|
string.insert(i, op[1])
|
||||||
|
@ -114,11 +112,12 @@ if __name__ == '__main__':
|
||||||
file.write('\t\t\t' + ''.join(string))
|
file.write('\t\t\t' + ''.join(string))
|
||||||
file.write('\r\n')
|
file.write('\r\n')
|
||||||
|
|
||||||
i += 1
|
|
||||||
cost += 2
|
cost += 2
|
||||||
|
|
||||||
print ''.join(string)
|
i += 1
|
||||||
print 'Cost: ', cost
|
|
||||||
|
print(''.join(string))
|
||||||
|
print('Cost: ', cost)
|
||||||
|
|
||||||
file.write('\r\nMinimum cost: ' + str(cost))
|
file.write('\r\nMinimum cost: ' + str(cost))
|
||||||
file.close()
|
file.close()
|
Loading…
Reference in New Issue
Block a user