mirror of
https://github.com/TheAlgorithms/Python.git
synced 2025-01-19 00:37:02 +00:00
Add type hints to strings/min_cost_string_conversion.py (#2337)
* done
* add types for local variables
* Revert "add types for local variables"
This reverts commit 971c15673b
.
* rename variables
* Update strings/min_cost_string_conversion.py
Co-authored-by: Christian Clauss <cclauss@me.com>
* rename strings
* use flake8
* Update strings/min_cost_string_conversion.py
Co-authored-by: Christian Clauss <cclauss@me.com>
This commit is contained in:
parent
2fa009aa53
commit
2388bf4e17
|
@ -1,55 +1,67 @@
|
||||||
|
from typing import List, Tuple
|
||||||
|
|
||||||
"""
|
"""
|
||||||
Algorithm for calculating the most cost-efficient sequence for converting one string
|
Algorithm for calculating the most cost-efficient sequence for converting one string
|
||||||
into another.
|
into another.
|
||||||
The only allowed operations are
|
The only allowed operations are
|
||||||
---Copy character with cost cC
|
--- Cost to copy a character is copy_cost
|
||||||
---Replace character with cost cR
|
--- Cost to replace a character is replace_cost
|
||||||
---Delete character with cost cD
|
--- Cost to delete a character is delete_cost
|
||||||
---Insert character with cost cI
|
--- Cost to insert a character is insert_cost
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
|
||||||
def compute_transform_tables(X, Y, cC, cR, cD, cI):
|
def compute_transform_tables(
|
||||||
X = list(X)
|
source_string: str,
|
||||||
Y = list(Y)
|
destination_string: str,
|
||||||
m = len(X)
|
copy_cost: int,
|
||||||
n = len(Y)
|
replace_cost: int,
|
||||||
|
delete_cost: int,
|
||||||
|
insert_cost: int,
|
||||||
|
) -> Tuple[List[int], List[str]]:
|
||||||
|
source_seq = list(source_string)
|
||||||
|
destination_seq = list(destination_string)
|
||||||
|
len_source_seq = len(source_seq)
|
||||||
|
len_destination_seq = len(destination_seq)
|
||||||
|
|
||||||
costs = [[0 for _ in range(n + 1)] for _ in range(m + 1)]
|
costs = [
|
||||||
ops = [[0 for _ in range(n + 1)] for _ in range(m + 1)]
|
[0 for _ in range(len_destination_seq + 1)] for _ in range(len_source_seq + 1)
|
||||||
|
]
|
||||||
|
ops = [
|
||||||
|
[0 for _ in range(len_destination_seq + 1)] for _ in range(len_source_seq + 1)
|
||||||
|
]
|
||||||
|
|
||||||
for i in range(1, m + 1):
|
for i in range(1, len_source_seq + 1):
|
||||||
costs[i][0] = i * cD
|
costs[i][0] = i * delete_cost
|
||||||
ops[i][0] = "D%c" % X[i - 1]
|
ops[i][0] = "D%c" % source_seq[i - 1]
|
||||||
|
|
||||||
for i in range(1, n + 1):
|
for i in range(1, len_destination_seq + 1):
|
||||||
costs[0][i] = i * cI
|
costs[0][i] = i * insert_cost
|
||||||
ops[0][i] = "I%c" % Y[i - 1]
|
ops[0][i] = "I%c" % destination_seq[i - 1]
|
||||||
|
|
||||||
for i in range(1, m + 1):
|
for i in range(1, len_source_seq + 1):
|
||||||
for j in range(1, n + 1):
|
for j in range(1, len_destination_seq + 1):
|
||||||
if X[i - 1] == Y[j - 1]:
|
if source_seq[i - 1] == destination_seq[j - 1]:
|
||||||
costs[i][j] = costs[i - 1][j - 1] + cC
|
costs[i][j] = costs[i - 1][j - 1] + copy_cost
|
||||||
ops[i][j] = "C%c" % X[i - 1]
|
ops[i][j] = "C%c" % source_seq[i - 1]
|
||||||
else:
|
else:
|
||||||
costs[i][j] = costs[i - 1][j - 1] + cR
|
costs[i][j] = costs[i - 1][j - 1] + replace_cost
|
||||||
ops[i][j] = "R%c" % X[i - 1] + str(Y[j - 1])
|
ops[i][j] = "R%c" % source_seq[i - 1] + str(destination_seq[j - 1])
|
||||||
|
|
||||||
if costs[i - 1][j] + cD < costs[i][j]:
|
if costs[i - 1][j] + delete_cost < costs[i][j]:
|
||||||
costs[i][j] = costs[i - 1][j] + cD
|
costs[i][j] = costs[i - 1][j] + delete_cost
|
||||||
ops[i][j] = "D%c" % X[i - 1]
|
ops[i][j] = "D%c" % source_seq[i - 1]
|
||||||
|
|
||||||
if costs[i][j - 1] + cI < costs[i][j]:
|
if costs[i][j - 1] + insert_cost < costs[i][j]:
|
||||||
costs[i][j] = costs[i][j - 1] + cI
|
costs[i][j] = costs[i][j - 1] + insert_cost
|
||||||
ops[i][j] = "I%c" % Y[j - 1]
|
ops[i][j] = "I%c" % destination_seq[j - 1]
|
||||||
|
|
||||||
return costs, ops
|
return costs, ops
|
||||||
|
|
||||||
|
|
||||||
def assemble_transformation(ops, i, j):
|
def assemble_transformation(ops: List[str], i: int, j: int) -> List[str]:
|
||||||
if i == 0 and j == 0:
|
if i == 0 and j == 0:
|
||||||
seq = []
|
return []
|
||||||
return seq
|
|
||||||
else:
|
else:
|
||||||
if ops[i][j][0] == "C" or ops[i][j][0] == "R":
|
if ops[i][j][0] == "C" or ops[i][j][0] == "R":
|
||||||
seq = assemble_transformation(ops, i - 1, j - 1)
|
seq = assemble_transformation(ops, i - 1, j - 1)
|
||||||
|
|
Loading…
Reference in New Issue
Block a user