From 2388bf4e17935a9860319831473e6a2e0f801b71 Mon Sep 17 00:00:00 2001 From: Eugeniy Orlov Date: Thu, 1 Oct 2020 04:04:31 +0300 Subject: [PATCH] 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 971c15673b2f0301f00b4464dfcd913a4dbb3b78. * rename variables * Update strings/min_cost_string_conversion.py Co-authored-by: Christian Clauss * rename strings * use flake8 * Update strings/min_cost_string_conversion.py Co-authored-by: Christian Clauss --- strings/min_cost_string_conversion.py | 78 +++++++++++++++------------ 1 file changed, 45 insertions(+), 33 deletions(-) diff --git a/strings/min_cost_string_conversion.py b/strings/min_cost_string_conversion.py index 34b42f3f0..e990aaa26 100644 --- a/strings/min_cost_string_conversion.py +++ b/strings/min_cost_string_conversion.py @@ -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) +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(n + 1)] for _ in range(m + 1)] - ops = [[0 for _ in range(n + 1)] for _ in range(m + 1)] + 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, m + 1): - costs[i][0] = i * cD - ops[i][0] = "D%c" % X[i - 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, n + 1): - costs[0][i] = i * cI - ops[0][i] = "I%c" % Y[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, 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] + 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)