-
-
Notifications
You must be signed in to change notification settings - Fork 46.9k
Added Python Implementation of Suffix Arrays and LCP Arrays #12161
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 8 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
6132d40
Suffix Array and LCP implementation.py
putul03 06a7be7
Delete divide_and_conquer/Suffix Array and LCP implementation.py
putul03 1e8f767
Added Suffix Array and LCP implementation
putul03 0094577
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 123e6f0
Suffix Array and LCP implementation.py
putul03 848a358
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] d950f57
Delete divide_and_conquer/Suffix Array and LCP implementation.py
putul03 dae072c
Suffix Array and LCP Array Implementation
putul03 70c3869
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 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 |
---|---|---|
@@ -0,0 +1,62 @@ | ||
#!/usr/bin/env python3 | ||
|
||
def build_suffix_array(s: str) -> list[int]: | ||
""" | ||
Build the suffix array for the given string. | ||
|
||
Parameters: | ||
s (str): The input string. | ||
|
||
Returns: | ||
list[int]: The suffix array (a list of starting indices of | ||
suffixes in sorted order). | ||
""" | ||
suffixes = [(s[i:], i) for i in range(len(s))] | ||
suffixes.sort() # Sort the suffixes lexicographically | ||
suffix_array = [suffix[1] for suffix in suffixes] | ||
return suffix_array | ||
|
||
def build_lcp_array(s: str, suffix_array: list[int]) -> list[int]: | ||
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. As there is no test file in this pull request nor any test function or class in the file Please provide descriptive name for the parameter: |
||
""" | ||
Build the LCP array for the given string and suffix array. | ||
|
||
Parameters: | ||
s (str): The input string. | ||
suffix_array (list[int]): The suffix array. | ||
|
||
Returns: | ||
list[int]: The LCP array. | ||
""" | ||
n = len(s) | ||
rank = [0] * n | ||
lcp = [0] * n | ||
|
||
# Compute the rank of each suffix | ||
for i, suffix_index in enumerate(suffix_array): | ||
rank[suffix_index] = i | ||
|
||
# Compute the LCP array | ||
h = 0 | ||
for i in range(n): | ||
if rank[i] > 0: | ||
j = suffix_array[rank[i] - 1] | ||
while (i + h < n) and (j + h < n) and (s[i + h] == s[j + h]): | ||
h += 1 | ||
lcp[rank[i]] = h | ||
if h > 0: | ||
h -= 1 # Decrease h for the next suffix | ||
return lcp | ||
|
||
# Example usage | ||
if __name__ == "__main__": | ||
s = "banana" | ||
suffix_array = build_suffix_array(s) | ||
lcp_array = build_lcp_array(s, suffix_array) | ||
|
||
print("Suffix Array:") | ||
for i in range(len(suffix_array)): | ||
print(f"{suffix_array[i]}: {s[suffix_array[i]:]}") | ||
|
||
print("\nLCP Array:") | ||
for i in range(1, len(lcp_array)): | ||
print(f"LCP between {s[suffix_array[i - 1]:]} and {s[suffix_array[i]]}: {lcp_array[i]}") | ||
Oops, something went wrong.
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.
As there is no test file in this pull request nor any test function or class in the file
divide_and_conquer/suffix_array_lcp.py
, please provide doctest for the functionbuild_suffix_array
Please provide descriptive name for the parameter:
s