|
| 1 | +from typing import List, Tuple |
| 2 | + |
1 | 3 | """
|
2 | 4 | Algorithm for calculating the most cost-efficient sequence for converting one string
|
3 | 5 | into another.
|
4 | 6 | 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 |
9 | 11 | """
|
10 | 12 |
|
11 | 13 |
|
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] |
34 | 47 | 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]) |
37 | 50 |
|
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] |
41 | 54 |
|
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] |
45 | 58 |
|
46 | 59 | return costs, ops
|
47 | 60 |
|
48 | 61 |
|
49 |
| -def assemble_transformation(ops, i, j): |
| 62 | +def assemble_transformation(ops: List[str], i: int, j: int) -> List[str]: |
50 | 63 | if i == 0 and j == 0:
|
51 |
| - seq = [] |
52 |
| - return seq |
| 64 | + return [] |
53 | 65 | else:
|
54 | 66 | if ops[i][j][0] == "C" or ops[i][j][0] == "R":
|
55 | 67 | seq = assemble_transformation(ops, i - 1, j - 1)
|
|
0 commit comments