Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit a4073ca

Browse files
authoredOct 19, 2024··
suffix_array_lcp.py
1 parent 1b37c1c commit a4073ca

File tree

1 file changed

+16
-19
lines changed

1 file changed

+16
-19
lines changed
 

‎divide_and_conquer/suffix_array_lcp.py

Lines changed: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
import doctest
44

5-
65
def build_suffix_array(input_string: str) -> list[int]:
76
"""
87
Build the suffix array for the given string.
@@ -14,7 +13,7 @@ def build_suffix_array(input_string: str) -> list[int]:
1413
list[int]: The suffix array (a list of starting indices of
1514
suffixes in sorted order).
1615
17-
Example:
16+
Examples:
1817
>>> build_suffix_array("banana")
1918
[5, 3, 1, 0, 4, 2]
2019
"""
@@ -23,7 +22,6 @@ def build_suffix_array(input_string: str) -> list[int]:
2322
suffix_array = [suffix[1] for suffix in suffixes]
2423
return suffix_array
2524

26-
2725
def build_lcp_array(input_string: str, suffix_array: list[int]) -> list[int]:
2826
"""
2927
Build the LCP array for the given string and suffix array.
@@ -35,9 +33,9 @@ def build_lcp_array(input_string: str, suffix_array: list[int]) -> list[int]:
3533
Returns:
3634
list[int]: The LCP array.
3735
38-
Example:
39-
>>> suffix_arr = build_suffix_array("banana")
40-
>>> build_lcp_array("banana", suffix_arr)
36+
Examples:
37+
>>> suffix_array = build_suffix_array("banana")
38+
>>> build_lcp_array("banana", suffix_array)
4139
[0, 1, 3, 0, 0, 2]
4240
"""
4341
n = len(input_string)
@@ -53,30 +51,29 @@ def build_lcp_array(input_string: str, suffix_array: list[int]) -> list[int]:
5351
for i in range(n):
5452
if rank[i] > 0:
5553
j = suffix_array[rank[i] - 1]
56-
while (
57-
(i + h < n)
58-
and (j + h < n)
59-
and (input_string[i + h] == input_string[j + h])
60-
):
54+
while (i + h < n) and (j + h < n) and (input_string[i + h] == input_string[j + h]):
6155
h += 1
6256
lcp[rank[i]] = h
6357
if h > 0:
6458
h -= 1 # Decrease h for the next suffix
6559
return lcp
6660

67-
6861
# Example usage
6962
if __name__ == "__main__":
70-
test_string = "banana"
71-
suffix_array = build_suffix_array(test_string)
72-
lcp_array = build_lcp_array(test_string, suffix_array)
63+
s = "banana"
64+
suffix_array = build_suffix_array(s)
65+
lcp_array = build_lcp_array(s, suffix_array)
7366

7467
print("Suffix Array:")
7568
for i in range(len(suffix_array)):
76-
print(f"{suffix_array[i]}: {test_string[suffix_array[i]:]}")
69+
print(f"{suffix_array[i]}: {s[suffix_array[i]:]}")
7770

7871
print("\nLCP Array:")
7972
for i in range(1, len(lcp_array)):
80-
print(
81-
f"LCP between {test_string[suffix_array[i - 1]:]} and {test_string[suffix_array[i]]}: {lcp_array[i]}"
82-
)
73+
lcp_info = (f"LCP between {s[suffix_array[i - 1]:]} and "
74+
f"{s[suffix_array[i]]}: {lcp_array[i]}")
75+
print(lcp_info)
76+
77+
# Run doctests
78+
if __name__ == "__main__":
79+
doctest.testmod()

0 commit comments

Comments
 (0)
Please sign in to comment.