-
-
Notifications
You must be signed in to change notification settings - Fork 46.8k
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
poyea
merged 8 commits into
TheAlgorithms:master
from
Sonic0588:sonic0588-type_hints_min_cost_string_conversion
Oct 1, 2020
Merged
Changes from 3 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
a2c790e
done
Sonic0588 971c156
add types for local variables
Sonic0588 4e00372
Revert "add types for local variables"
Sonic0588 979d39b
rename variables
Sonic0588 d2d97ff
Update strings/min_cost_string_conversion.py
Sonic0588 62ae900
rename strings
Sonic0588 4f34549
use flake8
Sonic0588 511b270
Update strings/min_cost_string_conversion.py
cclauss File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
@@ -4,48 +4,53 @@ | |||||||||
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 | ||||||||||
---Copy character with copy_cost | ||||||||||
---Replace character with replace_cost | ||||||||||
---Delete character with delete_cost | ||||||||||
---Insert character with insert_cost | ||||||||||
""" | ||||||||||
|
||||||||||
|
||||||||||
def compute_transform_tables( | ||||||||||
X: str, Y: str, cC: int, cR: int, cD: int, cI: int | ||||||||||
first_string: str, | ||||||||||
second_string: str, | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done it |
||||||||||
copy_cost: int, | ||||||||||
replace_cost: int, | ||||||||||
delete_cost: int, | ||||||||||
insert_cost: int, | ||||||||||
) -> Tuple[List[int], List[str]]: | ||||||||||
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] | ||||||||||
first_seq = list(first_string) | ||||||||||
second_seq = list(second_string) | ||||||||||
len_first_seq = len(first_seq) | ||||||||||
len_second_seq = len(second_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)] | ||||||||||
|
||||||||||
for i in range(1, len_first_seq + 1): | ||||||||||
costs[i][0] = i * delete_cost | ||||||||||
ops[i][0] = "D%c" % first_seq[i - 1] | ||||||||||
|
||||||||||
for i in range(1, len_second_seq + 1): | ||||||||||
costs[0][i] = i * insert_cost | ||||||||||
ops[0][i] = "I%c" % second_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]: | ||||||||||
costs[i][j] = costs[i - 1][j - 1] + copy_cost | ||||||||||
ops[i][j] = "C%c" % first_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" % first_seq[i - 1] + str(second_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" % first_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" % second_seq[j - 1] | ||||||||||
|
||||||||||
return costs, ops | ||||||||||
|
||||||||||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.