Skip to content

Commit f848971

Browse files
Sonic0588cclauss
authored andcommitted
Add type hints to strings/min_cost_string_conversion.py (TheAlgorithms#2337)
* done * add types for local variables * Revert "add types for local variables" This reverts commit 971c156. * rename variables * Update strings/min_cost_string_conversion.py Co-authored-by: Christian Clauss <[email protected]> * rename strings * use flake8 * Update strings/min_cost_string_conversion.py Co-authored-by: Christian Clauss <[email protected]>
1 parent bc9892b commit f848971

File tree

1 file changed

+49
-37
lines changed

1 file changed

+49
-37
lines changed

Diff for: strings/min_cost_string_conversion.py

+49-37
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,67 @@
1+
from typing import List, Tuple
2+
13
"""
24
Algorithm for calculating the most cost-efficient sequence for converting one string
35
into another.
46
The only allowed operations are
5-
---Copy character with cost cC
6-
---Replace character with cost cR
7-
---Delete character with cost cD
8-
---Insert character with cost cI
7+
--- Cost to copy a character is copy_cost
8+
--- Cost to replace a character is replace_cost
9+
--- Cost to delete a character is delete_cost
10+
--- Cost to insert a character is insert_cost
911
"""
1012

1113

12-
def compute_transform_tables(X, Y, cC, cR, cD, cI):
13-
X = list(X)
14-
Y = list(Y)
15-
m = len(X)
16-
n = len(Y)
17-
18-
costs = [[0 for _ in range(n + 1)] for _ in range(m + 1)]
19-
ops = [[0 for _ in range(n + 1)] for _ in range(m + 1)]
20-
21-
for i in range(1, m + 1):
22-
costs[i][0] = i * cD
23-
ops[i][0] = "D%c" % X[i - 1]
24-
25-
for i in range(1, n + 1):
26-
costs[0][i] = i * cI
27-
ops[0][i] = "I%c" % Y[i - 1]
28-
29-
for i in range(1, m + 1):
30-
for j in range(1, n + 1):
31-
if X[i - 1] == Y[j - 1]:
32-
costs[i][j] = costs[i - 1][j - 1] + cC
33-
ops[i][j] = "C%c" % X[i - 1]
14+
def compute_transform_tables(
15+
source_string: str,
16+
destination_string: str,
17+
copy_cost: int,
18+
replace_cost: int,
19+
delete_cost: int,
20+
insert_cost: int,
21+
) -> Tuple[List[int], List[str]]:
22+
source_seq = list(source_string)
23+
destination_seq = list(destination_string)
24+
len_source_seq = len(source_seq)
25+
len_destination_seq = len(destination_seq)
26+
27+
costs = [
28+
[0 for _ in range(len_destination_seq + 1)] for _ in range(len_source_seq + 1)
29+
]
30+
ops = [
31+
[0 for _ in range(len_destination_seq + 1)] for _ in range(len_source_seq + 1)
32+
]
33+
34+
for i in range(1, len_source_seq + 1):
35+
costs[i][0] = i * delete_cost
36+
ops[i][0] = "D%c" % source_seq[i - 1]
37+
38+
for i in range(1, len_destination_seq + 1):
39+
costs[0][i] = i * insert_cost
40+
ops[0][i] = "I%c" % destination_seq[i - 1]
41+
42+
for i in range(1, len_source_seq + 1):
43+
for j in range(1, len_destination_seq + 1):
44+
if source_seq[i - 1] == destination_seq[j - 1]:
45+
costs[i][j] = costs[i - 1][j - 1] + copy_cost
46+
ops[i][j] = "C%c" % source_seq[i - 1]
3447
else:
35-
costs[i][j] = costs[i - 1][j - 1] + cR
36-
ops[i][j] = "R%c" % X[i - 1] + str(Y[j - 1])
48+
costs[i][j] = costs[i - 1][j - 1] + replace_cost
49+
ops[i][j] = "R%c" % source_seq[i - 1] + str(destination_seq[j - 1])
3750

38-
if costs[i - 1][j] + cD < costs[i][j]:
39-
costs[i][j] = costs[i - 1][j] + cD
40-
ops[i][j] = "D%c" % X[i - 1]
51+
if costs[i - 1][j] + delete_cost < costs[i][j]:
52+
costs[i][j] = costs[i - 1][j] + delete_cost
53+
ops[i][j] = "D%c" % source_seq[i - 1]
4154

42-
if costs[i][j - 1] + cI < costs[i][j]:
43-
costs[i][j] = costs[i][j - 1] + cI
44-
ops[i][j] = "I%c" % Y[j - 1]
55+
if costs[i][j - 1] + insert_cost < costs[i][j]:
56+
costs[i][j] = costs[i][j - 1] + insert_cost
57+
ops[i][j] = "I%c" % destination_seq[j - 1]
4558

4659
return costs, ops
4760

4861

49-
def assemble_transformation(ops, i, j):
62+
def assemble_transformation(ops: List[str], i: int, j: int) -> List[str]:
5063
if i == 0 and j == 0:
51-
seq = []
52-
return seq
64+
return []
5365
else:
5466
if ops[i][j][0] == "C" or ops[i][j][0] == "R":
5567
seq = assemble_transformation(ops, i - 1, j - 1)

0 commit comments

Comments
 (0)