Skip to content

Commit 35dd529

Browse files
HetarthJainpre-commit-ci[bot]cclauss
authored
Returning Index instead of boolean in knuth_morris_pratt (kmp) function, making it compatible with str.find(). (TheAlgorithms#9083)
* Update knuth_morris_pratt.py - changed Boolean to Index * Update knuth_morris_pratt.py * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Update knuth_morris_pratt.py * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Update knuth_morris_pratt.py * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Update back_propagation_neural_network.py * Update back_propagation_neural_network.py * Update strings/knuth_morris_pratt.py * Update knuth_morris_pratt.py * Update knuth_morris_pratt.py --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Christian Clauss <[email protected]>
1 parent 38c2b83 commit 35dd529

File tree

1 file changed

+25
-8
lines changed

1 file changed

+25
-8
lines changed

Diff for: strings/knuth_morris_pratt.py

+25-8
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from __future__ import annotations
22

33

4-
def kmp(pattern: str, text: str) -> bool:
4+
def knuth_morris_pratt(text: str, pattern: str) -> int:
55
"""
66
The Knuth-Morris-Pratt Algorithm for finding a pattern within a piece of text
77
with complexity O(n + m)
@@ -14,6 +14,12 @@ def kmp(pattern: str, text: str) -> bool:
1414
2) Step through the text one character at a time and compare it to a character in
1515
the pattern updating our location within the pattern if necessary
1616
17+
>>> kmp = "knuth_morris_pratt"
18+
>>> all(
19+
... knuth_morris_pratt(kmp, s) == kmp.find(s)
20+
... for s in ("kn", "h_m", "rr", "tt", "not there")
21+
... )
22+
True
1723
"""
1824

1925
# 1) Construct the failure array
@@ -24,7 +30,7 @@ def kmp(pattern: str, text: str) -> bool:
2430
while i < len(text):
2531
if pattern[j] == text[i]:
2632
if j == (len(pattern) - 1):
27-
return True
33+
return i - j
2834
j += 1
2935

3036
# if this is a prefix in our pattern
@@ -33,7 +39,7 @@ def kmp(pattern: str, text: str) -> bool:
3339
j = failure[j - 1]
3440
continue
3541
i += 1
36-
return False
42+
return -1
3743

3844

3945
def get_failure_array(pattern: str) -> list[int]:
@@ -57,27 +63,38 @@ def get_failure_array(pattern: str) -> list[int]:
5763

5864

5965
if __name__ == "__main__":
66+
import doctest
67+
68+
doctest.testmod()
69+
6070
# Test 1)
6171
pattern = "abc1abc12"
6272
text1 = "alskfjaldsabc1abc1abc12k23adsfabcabc"
6373
text2 = "alskfjaldsk23adsfabcabc"
64-
assert kmp(pattern, text1) and not kmp(pattern, text2)
74+
assert knuth_morris_pratt(text1, pattern) and knuth_morris_pratt(text2, pattern)
6575

6676
# Test 2)
6777
pattern = "ABABX"
6878
text = "ABABZABABYABABX"
69-
assert kmp(pattern, text)
79+
assert knuth_morris_pratt(text, pattern)
7080

7181
# Test 3)
7282
pattern = "AAAB"
7383
text = "ABAAAAAB"
74-
assert kmp(pattern, text)
84+
assert knuth_morris_pratt(text, pattern)
7585

7686
# Test 4)
7787
pattern = "abcdabcy"
7888
text = "abcxabcdabxabcdabcdabcy"
79-
assert kmp(pattern, text)
89+
assert knuth_morris_pratt(text, pattern)
90+
91+
# Test 5) -> Doctests
92+
kmp = "knuth_morris_pratt"
93+
assert all(
94+
knuth_morris_pratt(kmp, s) == kmp.find(s)
95+
for s in ("kn", "h_m", "rr", "tt", "not there")
96+
)
8097

81-
# Test 5)
98+
# Test 6)
8299
pattern = "aabaabaaa"
83100
assert get_failure_array(pattern) == [0, 1, 0, 1, 2, 3, 4, 5, 2]

0 commit comments

Comments
 (0)