From 13749f335d175777f85b0c6e8dfc8732722189a3 Mon Sep 17 00:00:00 2001 From: Akashram28 Date: Sat, 23 Sep 2023 15:17:42 +0530 Subject: [PATCH 1/8] Changed knuth_morris_pratt to be consistent with str.find() --- strings/knuth_morris_pratt.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/strings/knuth_morris_pratt.py b/strings/knuth_morris_pratt.py index a488c171a93b..b116ed067343 100644 --- a/strings/knuth_morris_pratt.py +++ b/strings/knuth_morris_pratt.py @@ -24,7 +24,7 @@ def kmp(pattern: str, text: str) -> bool: while i < len(text): if pattern[j] == text[i]: if j == (len(pattern) - 1): - return True + return i-j j += 1 # if this is a prefix in our pattern @@ -33,7 +33,7 @@ def kmp(pattern: str, text: str) -> bool: j = failure[j - 1] continue i += 1 - return False + return -1 def get_failure_array(pattern: str) -> list[int]: @@ -61,22 +61,22 @@ def get_failure_array(pattern: str) -> list[int]: pattern = "abc1abc12" text1 = "alskfjaldsabc1abc1abc12k23adsfabcabc" text2 = "alskfjaldsk23adsfabcabc" - assert kmp(pattern, text1) and not kmp(pattern, text2) + print(kmp(pattern, text1),kmp(pattern, text2)) # Test 2) pattern = "ABABX" text = "ABABZABABYABABX" - assert kmp(pattern, text) + print(kmp(pattern, text)) # Test 3) pattern = "AAAB" text = "ABAAAAAB" - assert kmp(pattern, text) + print(kmp(pattern, text)) # Test 4) pattern = "abcdabcy" text = "abcxabcdabxabcdabcdabcy" - assert kmp(pattern, text) + print(kmp(pattern, text)) # Test 5) pattern = "aabaabaaa" From 85aaab597a863951765d85fb7d469409f32e04e6 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sat, 23 Sep 2023 10:00:12 +0000 Subject: [PATCH 2/8] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- strings/knuth_morris_pratt.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/strings/knuth_morris_pratt.py b/strings/knuth_morris_pratt.py index b116ed067343..cd2c10200151 100644 --- a/strings/knuth_morris_pratt.py +++ b/strings/knuth_morris_pratt.py @@ -24,7 +24,7 @@ def kmp(pattern: str, text: str) -> bool: while i < len(text): if pattern[j] == text[i]: if j == (len(pattern) - 1): - return i-j + return i - j j += 1 # if this is a prefix in our pattern @@ -61,7 +61,7 @@ def get_failure_array(pattern: str) -> list[int]: pattern = "abc1abc12" text1 = "alskfjaldsabc1abc1abc12k23adsfabcabc" text2 = "alskfjaldsk23adsfabcabc" - print(kmp(pattern, text1),kmp(pattern, text2)) + print(kmp(pattern, text1), kmp(pattern, text2)) # Test 2) pattern = "ABABX" From 212e35238f696874d9349b9c638cd94485ee864d Mon Sep 17 00:00:00 2001 From: Akashram28 Date: Sat, 23 Sep 2023 15:47:25 +0530 Subject: [PATCH 3/8] knuth_morris_pratt now returns integer index instead of boolean --- strings/knuth_morris_pratt.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/strings/knuth_morris_pratt.py b/strings/knuth_morris_pratt.py index b116ed067343..ee52ee0ee46f 100644 --- a/strings/knuth_morris_pratt.py +++ b/strings/knuth_morris_pratt.py @@ -1,7 +1,7 @@ from __future__ import annotations -def kmp(pattern: str, text: str) -> bool: +def kmp(pattern: str, text: str) -> int: """ The Knuth-Morris-Pratt Algorithm for finding a pattern within a piece of text with complexity O(n + m) From c3aeabf61d6e92d1437286fda723efdceb45d32e Mon Sep 17 00:00:00 2001 From: Akashram <68014251+Akashram28@users.noreply.github.com> Date: Sat, 23 Sep 2023 16:04:46 +0530 Subject: [PATCH 4/8] Update strings/knuth_morris_pratt.py Co-authored-by: Rohan Anand <96521078+rohan472000@users.noreply.github.com> --- strings/knuth_morris_pratt.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/strings/knuth_morris_pratt.py b/strings/knuth_morris_pratt.py index 2b68ec8fd8de..83776b71a22b 100644 --- a/strings/knuth_morris_pratt.py +++ b/strings/knuth_morris_pratt.py @@ -1,7 +1,7 @@ from __future__ import annotations -def kmp(pattern: str, text: str) -> int: +def knuth_morris_pratt(pattern: str, text: str) -> int: """ The Knuth-Morris-Pratt Algorithm for finding a pattern within a piece of text with complexity O(n + m) From e9babb76f505dcee95c68a04856d690ad760fb11 Mon Sep 17 00:00:00 2001 From: Akashram28 Date: Sat, 23 Sep 2023 16:08:11 +0530 Subject: [PATCH 5/8] Changed kmp() to knuth_morris_pratt() --- strings/knuth_morris_pratt.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/strings/knuth_morris_pratt.py b/strings/knuth_morris_pratt.py index 83776b71a22b..ec2112d50a63 100644 --- a/strings/knuth_morris_pratt.py +++ b/strings/knuth_morris_pratt.py @@ -61,22 +61,22 @@ def get_failure_array(pattern: str) -> list[int]: pattern = "abc1abc12" text1 = "alskfjaldsabc1abc1abc12k23adsfabcabc" text2 = "alskfjaldsk23adsfabcabc" - print(kmp(pattern, text1), kmp(pattern, text2)) + print(knuth_morris_pratt(pattern, text1), knuth_morris_pratt(pattern, text2)) # Test 2) pattern = "ABABX" text = "ABABZABABYABABX" - print(kmp(pattern, text)) + print(knuth_morris_pratt(pattern, text)) # Test 3) pattern = "AAAB" text = "ABAAAAAB" - print(kmp(pattern, text)) + print(knuth_morris_pratt(pattern, text)) # Test 4) pattern = "abcdabcy" text = "abcxabcdabxabcdabcdabcy" - print(kmp(pattern, text)) + print(knuth_morris_pratt(pattern, text)) # Test 5) pattern = "aabaabaaa" From bd862b07567985fc11675b9c664ec0b6ae862517 Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Sat, 23 Sep 2023 14:44:00 +0200 Subject: [PATCH 6/8] Add doctests --- strings/knuth_morris_pratt.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/strings/knuth_morris_pratt.py b/strings/knuth_morris_pratt.py index ec2112d50a63..e1ee7c02c440 100644 --- a/strings/knuth_morris_pratt.py +++ b/strings/knuth_morris_pratt.py @@ -14,6 +14,10 @@ def knuth_morris_pratt(pattern: str, text: str) -> int: 2) Step through the text one character at a time and compare it to a character in the pattern updating our location within the pattern if necessary + >>> kmp = "knuth_morris_pratt" + >>> all(knuth_morris_pratt(kmp, s) == kmp.find(s) for s in ( + ... "kn", "h_m", "rr", "tt", "not there" + ... )) """ # 1) Construct the failure array From c6eb9a8f160372ee847c742619b2a42ced0fcf55 Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Sat, 23 Sep 2023 14:51:44 +0200 Subject: [PATCH 7/8] Update knuth_morris_pratt.py --- strings/knuth_morris_pratt.py | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/strings/knuth_morris_pratt.py b/strings/knuth_morris_pratt.py index e1ee7c02c440..7a508c56514f 100644 --- a/strings/knuth_morris_pratt.py +++ b/strings/knuth_morris_pratt.py @@ -15,9 +15,22 @@ def knuth_morris_pratt(pattern: str, text: str) -> int: the pattern updating our location within the pattern if necessary >>> kmp = "knuth_morris_pratt" + >>> knuth_morris_pratt(kmp, "kn") == kmp.find("kn") + True + >>> knuth_morris_pratt(kmp, "h_m") == kmp.find("h_m") + True + >>> knuth_morris_pratt(kmp, "rr") == kmp.find("rr") + True + >>> knuth_morris_pratt(kmp, "tt") == kmp.find("tt") + True + >>> knuth_morris_pratt(kmp, "not there") == kmp.find("not there") + True + + # A condensed version... >>> all(knuth_morris_pratt(kmp, s) == kmp.find(s) for s in ( ... "kn", "h_m", "rr", "tt", "not there" ... )) + True """ # 1) Construct the failure array From ba4dbe082d9d3e62fd035c281fb47c4ef421f52b Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sat, 23 Sep 2023 12:52:26 +0000 Subject: [PATCH 8/8] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- strings/knuth_morris_pratt.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/strings/knuth_morris_pratt.py b/strings/knuth_morris_pratt.py index 7a508c56514f..2807948e6ffd 100644 --- a/strings/knuth_morris_pratt.py +++ b/strings/knuth_morris_pratt.py @@ -25,7 +25,7 @@ def knuth_morris_pratt(pattern: str, text: str) -> int: True >>> knuth_morris_pratt(kmp, "not there") == kmp.find("not there") True - + # A condensed version... >>> all(knuth_morris_pratt(kmp, s) == kmp.find(s) for s in ( ... "kn", "h_m", "rr", "tt", "not there"