Skip to content

Commit 6b60a6b

Browse files
authored
Merge pull request TheAlgorithms#273 from daniel-s-ingram/master
Algorithm for transforming one string into another in the most cost-efficient way
2 parents c461b34 + 070ebb3 commit 6b60a6b

File tree

3 files changed

+161
-0
lines changed

3 files changed

+161
-0
lines changed

Project Euler/Problem 22/p022_names.txt

+1
Large diffs are not rendered by default.

Project Euler/Problem 22/sol1.py

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# -*- coding: latin-1 -*-
2+
from __future__ import print_function
3+
'''
4+
Name scores
5+
Problem 22
6+
7+
Using names.txt (right click and 'Save Link/Target As...'), a 46K text file containing over five-thousand first names, begin by sorting it
8+
into alphabetical order. Then working out the alphabetical value for each name, multiply this value by its alphabetical position in the list
9+
to obtain a name score.
10+
11+
For example, when the list is sorted into alphabetical order, COLIN, which is worth 3 + 15 + 12 + 9 + 14 = 53, is the 938th name in the list.
12+
So, COLIN would obtain a score of 938 × 53 = 49714.
13+
14+
What is the total of all the name scores in the file?
15+
'''
16+
try:
17+
xrange #Python 2
18+
except NameError:
19+
xrange = range #Python 3
20+
21+
with open('p022_names.txt') as file:
22+
names = str(file.readlines()[0])
23+
names = names.replace('"', '').split(',')
24+
25+
names.sort()
26+
27+
name_score = 0
28+
total_score = 0
29+
30+
for i, name in enumerate(names):
31+
for letter in name:
32+
name_score += ord(letter) - 64
33+
34+
total_score += (i+1)*name_score
35+
name_score = 0
36+
37+
print(total_score)

strings/min-cost-string-conversion.py

+123
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
from __future__ import print_function
2+
3+
try:
4+
xrange #Python 2
5+
except NameError:
6+
xrange = range #Python 3
7+
8+
'''
9+
Algorithm for calculating the most cost-efficient sequence for converting one string into another.
10+
The only allowed operations are
11+
---Copy character with cost cC
12+
---Replace character with cost cR
13+
---Delete character with cost cD
14+
---Insert character with cost cI
15+
'''
16+
def compute_transform_tables(X, Y, cC, cR, cD, cI):
17+
X = list(X)
18+
Y = list(Y)
19+
m = len(X)
20+
n = len(Y)
21+
22+
costs = [[0 for _ in xrange(n+1)] for _ in xrange(m+1)]
23+
ops = [[0 for _ in xrange(n+1)] for _ in xrange(m+1)]
24+
25+
for i in xrange(1, m+1):
26+
costs[i][0] = i*cD
27+
ops[i][0] = 'D%c' % X[i-1]
28+
29+
for i in xrange(1, n+1):
30+
costs[0][i] = i*cI
31+
ops[0][i] = 'I%c' % Y[i-1]
32+
33+
for i in xrange(1, m+1):
34+
for j in xrange(1, n+1):
35+
if X[i-1] == Y[j-1]:
36+
costs[i][j] = costs[i-1][j-1] + cC
37+
ops[i][j] = 'C%c' % X[i-1]
38+
else:
39+
costs[i][j] = costs[i-1][j-1] + cR
40+
ops[i][j] = 'R%c' % X[i-1] + str(Y[j-1])
41+
42+
if costs[i-1][j] + cD < costs[i][j]:
43+
costs[i][j] = costs[i-1][j] + cD
44+
ops[i][j] = 'D%c' % X[i-1]
45+
46+
if costs[i][j-1] + cI < costs[i][j]:
47+
costs[i][j] = costs[i][j-1] + cI
48+
ops[i][j] = 'I%c' % Y[j-1]
49+
50+
return costs, ops
51+
52+
def assemble_transformation(ops, i, j):
53+
if i == 0 and j == 0:
54+
seq = []
55+
return seq
56+
else:
57+
if ops[i][j][0] == 'C' or ops[i][j][0] == 'R':
58+
seq = assemble_transformation(ops, i-1, j-1)
59+
seq.append(ops[i][j])
60+
return seq
61+
elif ops[i][j][0] == 'D':
62+
seq = assemble_transformation(ops, i-1, j)
63+
seq.append(ops[i][j])
64+
return seq
65+
else:
66+
seq = assemble_transformation(ops, i, j-1)
67+
seq.append(ops[i][j])
68+
return seq
69+
70+
if __name__ == '__main__':
71+
from time import sleep
72+
_, operations = compute_transform_tables('Python', 'Algorithms', -1, 1, 2, 2)
73+
74+
m = len(operations)
75+
n = len(operations[0])
76+
sequence = assemble_transformation(operations, m-1, n-1)
77+
78+
file = open('min_cost.txt', 'w')
79+
80+
string = list('Python')
81+
i = 0
82+
cost = 0
83+
for op in sequence:
84+
print(''.join(string))
85+
86+
if op[0] == 'C':
87+
file.write('%-16s' % 'Copy %c' % op[1])
88+
file.write('\t\t\t' + ''.join(string))
89+
file.write('\r\n')
90+
91+
cost -= 1
92+
elif op[0] == 'R':
93+
string[i] = op[2]
94+
95+
file.write('%-16s' % ('Replace %c' % op[1] + ' with ' + str(op[2])))
96+
file.write('\t\t' + ''.join(string))
97+
file.write('\r\n')
98+
99+
cost += 1
100+
elif op[0] == 'D':
101+
string.pop(i)
102+
103+
file.write('%-16s' % 'Delete %c' % op[1])
104+
file.write('\t\t\t' + ''.join(string))
105+
file.write('\r\n')
106+
107+
cost += 2
108+
else:
109+
string.insert(i, op[1])
110+
111+
file.write('%-16s' % 'Insert %c' % op[1])
112+
file.write('\t\t\t' + ''.join(string))
113+
file.write('\r\n')
114+
115+
cost += 2
116+
117+
i += 1
118+
119+
print(''.join(string))
120+
print('Cost: ', cost)
121+
122+
file.write('\r\nMinimum cost: ' + str(cost))
123+
file.close()

0 commit comments

Comments
 (0)