-
-
Notifications
You must be signed in to change notification settings - Fork 46.6k
Edit_Distance algorithm for genetic string matching #10336
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
Closed
Closed
Changes from 7 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
f574ce1
Edit_Distance algorithm for genetic string matching
anshul-2010 caeda9f
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] f3303b0
Update edit_distance.py
anshul-2010 a6ef5bd
Update edit_distance.py
anshul-2010 00da496
Update edit_distance.py
anshul-2010 4d1fd42
Update edit_distance.py
anshul-2010 1455e30
Update edit_distance.py
anshul-2010 c97c94a
Update edit_distance.py
anshul-2010 a5d5316
Update edit_distance.py
anshul-2010 4d64c3e
Update edit_distance.py
anshul-2010 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 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 |
---|---|---|
@@ -0,0 +1,42 @@ | ||
def edit_distance(source: str, target: str): -> int | ||
""" | ||
Edit distance algorithm is a string metric, i.e., it is a way of quantifying | ||
how dissimilar two strings are to one another, that is measured by | ||
counting the minimum number of operations required to transform one string | ||
into another. | ||
In genetic algorithms consisting of A,T, G, and C nucleotides, this matching | ||
becomes essential in understanding the mutation in succesive genes. | ||
Hence, this algorithm comes in handy when we are trying to quantify the | ||
mutations in successive generations. | ||
Args: | ||
source (type __string__): This is the source string, the initial string with | ||
respect to which we are calculating the edit_distance for the target | ||
target (type __string__): This is the target string, which is formed after n | ||
number of operations performed on the source string. | ||
Assumptions: | ||
The cost of operations (insertion, deletion and subtraction) is all 1 | ||
Given two integers, return the sum. | ||
|
||
:param source: str | ||
:param target: str | ||
:return: int | ||
|
||
>>> edit_distance("GATTIC, GALTIC) | ||
1 | ||
""" | ||
delta = {True: 0, False: 1} # Substitution | ||
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. An error occurred while parsing the file: Traceback (most recent call last):
File "/opt/render/project/src/algorithms_keeper/parser/python_parser.py", line 146, in parse
reports = lint_file(
^^^^^^^^^^
libcst._exceptions.ParserSyntaxError: Syntax Error @ 19:4.
parser error: error at 18:3: expected INDENT
delta = {True: 0, False: 1} # Substitution
^ |
||
|
||
if len(source) == 0: | ||
return len(target) | ||
elif len(target) == 0: | ||
return len(source) | ||
|
||
return min( | ||
edit_distance(source[:-1], target[:-1]) + delta[source[-1] == target[-1]], | ||
edit_distance(source, target[:-1]) + 1, | ||
edit_distance(source[:-1], target) + 1, | ||
) | ||
|
||
|
||
print(edit_distance("ATCGCTG", "TAGCTAA")) | ||
# Answer is 4 |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
An error occurred while parsing the file:
genetic_algorithm/edit_distance.py