-
Notifications
You must be signed in to change notification settings - Fork 271
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
Adding Longest Common Subsequence #315
Changes from 71 commits
5b34a79
52f01c3
6610a7f
4c5c855
f93008a
6867add
628045c
09e5e42
74009e6
0055baa
9dec38c
708f6bc
27a5f1a
6ae5a0c
aaf529a
9f937d4
38ebcbe
9a77768
40350b4
a049c11
a990d46
e49d268
c6c5fdd
db70e68
9acbebf
57d7fbf
3569652
c9d4f9c
e1d817f
864e95c
570a287
cf91f8a
3c34257
1ae0e3d
89b903a
7a4581c
0c741ca
4835dd4
069613a
3f97409
2177e04
7e23b41
abe4362
0ad0b56
4e1e8d4
f8afd26
377bfd6
a1fd65a
7c4d918
c042722
ed7a059
9cda682
65d0f88
f22a7ac
5da091e
2c7afac
e8d13b0
f7c6463
b476021
923b1be
bcb678e
a6fec40
bc6cd31
96067eb
6df16fb
a73bc25
07fc3e9
86f389c
60cfdc2
8c26960
699b3b2
0ae9085
90f1089
2196ec2
2766287
a85e08b
478bf66
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -13,7 +13,8 @@ | |||||||||||||||
'counting_sort', | ||||||||||||||||
'bucket_sort', | ||||||||||||||||
'cocktail_shaker_sort', | ||||||||||||||||
'quick_sort' | ||||||||||||||||
'quick_sort', | ||||||||||||||||
'longest_common_subsequence' | ||||||||||||||||
] | ||||||||||||||||
|
||||||||||||||||
def _merge(array, sl, el, sr, er, end, comp): | ||||||||||||||||
|
@@ -722,3 +723,67 @@ def partition(low, high, pick_pivot_element): | |||||||||||||||
array._modify(force=True) | ||||||||||||||||
|
||||||||||||||||
return array | ||||||||||||||||
|
||||||||||||||||
def longest_common_subsequence(seq1, seq2) -> tuple: | ||||||||||||||||
""" | ||||||||||||||||
Implements Longest Common Subsequence | ||||||||||||||||
|
||||||||||||||||
Parameters | ||||||||||||||||
======== | ||||||||||||||||
|
||||||||||||||||
seq1: String or List or Tuple | ||||||||||||||||
seq2: String or List or Tuple | ||||||||||||||||
|
||||||||||||||||
Returns | ||||||||||||||||
======= | ||||||||||||||||
|
||||||||||||||||
output: tuple | ||||||||||||||||
(Length of LCS, Common Sequence) | ||||||||||||||||
Common Sequence will be of the same data type as seq1. | ||||||||||||||||
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
|
||||||||||||||||
|
||||||||||||||||
Examples | ||||||||||||||||
======== | ||||||||||||||||
|
||||||||||||||||
>>> from pydatastructs import longest_common_subsequence as LCS | ||||||||||||||||
>>> LCS("ABCDEF", "ABBCDDDE") | ||||||||||||||||
(5, 'ABCDE') | ||||||||||||||||
>>> arr1 = ['A', 'P', 'P'] | ||||||||||||||||
>>> arr2 = ['A', 'p', 'P', 'S', 'P'] | ||||||||||||||||
>>> LCS(arr1, arr2) | ||||||||||||||||
(3, ['A', 'P', 'P']) | ||||||||||||||||
|
||||||||||||||||
References | ||||||||||||||||
========== | ||||||||||||||||
|
||||||||||||||||
.. [1] https://en.wikipedia.org/wiki/Longest_common_subsequence_problem | ||||||||||||||||
""" | ||||||||||||||||
if not(isinstance(seq1, (str, tuple, list))): | ||||||||||||||||
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. A blank line after 759 and it isn't necessary to use brackets with not. 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. Can you just find all the issue and report me, as Gagandeep asked me to reduce the commits to reduce the resource used by travis 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. I will change this for now. 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.
Issues can only be reported when they arise. Can't report them before their occurance. Commits can be squashed into one so no worries. |
||||||||||||||||
raise TypeError("Only Strings, Tuple and List are allowed") | ||||||||||||||||
if not(isinstance(seq2, (str, tuple, list))): | ||||||||||||||||
raise TypeError("Only Strings, Tuple and List are allowed") | ||||||||||||||||
|
||||||||||||||||
row, col = len(seq1), len(seq2) | ||||||||||||||||
check_mat = [[0 for _ in range(col+1)] for x in range(row+1)] | ||||||||||||||||
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. What about using a nested |
||||||||||||||||
for i in range(row): | ||||||||||||||||
for j in range(col): | ||||||||||||||||
if (seq1[i] == seq2[j]): | ||||||||||||||||
check_mat[i+1][j+1] = check_mat[i][j]+1 | ||||||||||||||||
else: | ||||||||||||||||
check_mat[i+1][j+1] = max(check_mat[i+1][j], check_mat[i][j+1]) | ||||||||||||||||
|
||||||||||||||||
lcseq, lclen = [], check_mat[row][col] | ||||||||||||||||
while(row > 0 and col > 0): | ||||||||||||||||
if(check_mat[row][col] == check_mat[row][col-1]): | ||||||||||||||||
col -= 1 | ||||||||||||||||
elif(check_mat[row][col] == check_mat[row-1][col]): | ||||||||||||||||
row -= 1 | ||||||||||||||||
else: | ||||||||||||||||
lcseq.append(seq1[row-1]) | ||||||||||||||||
row -= 1 | ||||||||||||||||
col -= 1 | ||||||||||||||||
|
||||||||||||||||
if(type(seq1) == str): | ||||||||||||||||
lcseq = ''.join(lcseq) | ||||||||||||||||
if(type(seq1) == tuple): | ||||||||||||||||
lcseq = tuple(lcseq) | ||||||||||||||||
return (lclen, lcseq[::-1]) | ||||||||||||||||
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. This part is cryptic. We should keep the input types restricted to P.S. That is why doing some background lookups are preferred for discussing APIs rather than just directly coding out things and keep changing frequently. 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. Yeah next time onwards we discuss and start the implementation and I will try to implement the above in One dimensional |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,8 @@ | ||
from pydatastructs import ( | ||
merge_sort_parallel, DynamicOneDimensionalArray, | ||
OneDimensionalArray, brick_sort, brick_sort_parallel, | ||
heapsort, matrix_multiply_parallel, counting_sort, bucket_sort, cocktail_shaker_sort, quick_sort) | ||
heapsort, matrix_multiply_parallel, counting_sort, bucket_sort, cocktail_shaker_sort, quick_sort, longest_common_subsequence) | ||
|
||
|
||
from pydatastructs.utils.raises_util import raises | ||
import random | ||
|
@@ -100,3 +101,16 @@ def test_matrix_multiply_parallel(): | |
J = [[2, 1, 2], [1, 2, 1], [2, 2, 2]] | ||
output = matrix_multiply_parallel(I, J, num_threads=1) | ||
assert expected_result == output | ||
|
||
def test_longest_common_sequence(): | ||
expected_result = (5, 'ASCII') | ||
|
||
str1, str2 = 'AASCCII', 'ASSCIIII' | ||
output = longest_common_subsequence(str1, str2) | ||
assert expected_result == output | ||
|
||
expected_result = (3, ['O', 'V', 'A']) | ||
|
||
I, J = ['O', 'V', 'A', 'L'], ['F', 'O', 'R', 'V', 'A', 'E', 'W'] | ||
output = longest_common_subsequence(I, J) | ||
assert expected_result == output | ||
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. Add one test case for 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. Yeah sure that can be done |
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.