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 1 commit
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
Prev Previous commit
Next Next commit
rename strings
  • Loading branch information
Sonic0588 committed Aug 21, 2020
commit 62ae90003e9bc2fa18a71c62cfbb8ffa51c3f1ed
38 changes: 19 additions & 19 deletions strings/min_cost_string_conversion.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,45 +12,45 @@


def compute_transform_tables(
first_string: str,
second_string: str,
source_string: str,
destination_string: str,
copy_cost: int,
replace_cost: int,
delete_cost: int,
insert_cost: int,
) -> Tuple[List[int], List[str]]:
first_seq = list(first_string)
second_seq = list(second_string)
len_first_seq = len(first_seq)
len_second_seq = len(second_seq)
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_second_seq + 1)] for _ in range(len_first_seq + 1)]
ops = [[0 for _ in range(len_second_seq + 1)] for _ in range(len_first_seq + 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, len_first_seq + 1):
for i in range(1, len_source_seq + 1):
costs[i][0] = i * delete_cost
ops[i][0] = "D%c" % first_seq[i - 1]
ops[i][0] = "D%c" % source_seq[i - 1]

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

for i in range(1, len_first_seq + 1):
for j in range(1, len_second_seq + 1):
if first_seq[i - 1] == second_seq[j - 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" % first_seq[i - 1]
ops[i][j] = "C%c" % source_seq[i - 1]
else:
costs[i][j] = costs[i - 1][j - 1] + replace_cost
ops[i][j] = "R%c" % first_seq[i - 1] + str(second_seq[j - 1])
ops[i][j] = "R%c" % source_seq[i - 1] + str(destination_seq[j - 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" % first_seq[i - 1]
ops[i][j] = "D%c" % source_seq[i - 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" % second_seq[j - 1]
ops[i][j] = "I%c" % destination_seq[j - 1]

return costs, ops

Expand Down