Skip to content

added type hints to strings/min_cost_string_conversion.py #2337

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
86 changes: 49 additions & 37 deletions strings/min_cost_string_conversion.py
Original file line number Diff line number Diff line change
@@ -1,55 +1,67 @@
from typing import List, Tuple

"""
Algorithm for calculating the most cost-efficient sequence for converting one string
into another.
The only allowed operations are
---Copy character with cost cC
---Replace character with cost cR
---Delete character with cost cD
---Insert character with cost cI
--- Cost to copy a character is copy_cost
--- Cost to replace a character is replace_cost
--- Cost to delete a character is delete_cost
--- Cost to insert a character is insert_cost
"""


def compute_transform_tables(X, Y, cC, cR, cD, cI):
X = list(X)
Y = list(Y)
m = len(X)
n = len(Y)

costs = [[0 for _ in range(n + 1)] for _ in range(m + 1)]
ops = [[0 for _ in range(n + 1)] for _ in range(m + 1)]

for i in range(1, m + 1):
costs[i][0] = i * cD
ops[i][0] = "D%c" % X[i - 1]

for i in range(1, n + 1):
costs[0][i] = i * cI
ops[0][i] = "I%c" % Y[i - 1]

for i in range(1, m + 1):
for j in range(1, n + 1):
if X[i - 1] == Y[j - 1]:
costs[i][j] = costs[i - 1][j - 1] + cC
ops[i][j] = "C%c" % X[i - 1]
def compute_transform_tables(
source_string: str,
destination_string: str,
copy_cost: int,
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(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, len_source_seq + 1):
costs[i][0] = i * delete_cost
ops[i][0] = "D%c" % source_seq[i - 1]

for i in range(1, len_destination_seq + 1):
costs[0][i] = i * insert_cost
ops[0][i] = "I%c" % destination_seq[i - 1]

for i in range(1, len_source_seq + 1):
for j in range(1, len_destination_seq + 1):
if source_seq[i - 1] == destination_seq[j - 1]:
costs[i][j] = costs[i - 1][j - 1] + copy_cost
ops[i][j] = "C%c" % source_seq[i - 1]
else:
costs[i][j] = costs[i - 1][j - 1] + cR
ops[i][j] = "R%c" % X[i - 1] + str(Y[j - 1])
costs[i][j] = costs[i - 1][j - 1] + replace_cost
ops[i][j] = "R%c" % source_seq[i - 1] + str(destination_seq[j - 1])

if costs[i - 1][j] + cD < costs[i][j]:
costs[i][j] = costs[i - 1][j] + cD
ops[i][j] = "D%c" % X[i - 1]
if costs[i - 1][j] + delete_cost < costs[i][j]:
costs[i][j] = costs[i - 1][j] + delete_cost
ops[i][j] = "D%c" % source_seq[i - 1]

if costs[i][j - 1] + cI < costs[i][j]:
costs[i][j] = costs[i][j - 1] + cI
ops[i][j] = "I%c" % Y[j - 1]
if costs[i][j - 1] + insert_cost < costs[i][j]:
costs[i][j] = costs[i][j - 1] + insert_cost
ops[i][j] = "I%c" % destination_seq[j - 1]

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:
seq = []
return seq
return []
else:
if ops[i][j][0] == "C" or ops[i][j][0] == "R":
seq = assemble_transformation(ops, i - 1, j - 1)
Expand Down