From fc869ad45c36320895cc524f9fa0baed91048711 Mon Sep 17 00:00:00 2001 From: Hetarth Jain Date: Mon, 25 Sep 2023 00:19:39 +0530 Subject: [PATCH 01/12] Update knuth_morris_pratt.py - changed Boolean to Index --- 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 a488c171a93b..b3ba9244b507 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 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]: From ab8ce325316b8e8778db91f76b1de05258063eb4 Mon Sep 17 00:00:00 2001 From: Hetarth Jain Date: Mon, 25 Sep 2023 00:58:33 +0530 Subject: [PATCH 02/12] Update knuth_morris_pratt.py --- 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 b3ba9244b507..6894cf43e18a 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 knuth_morris_pratt(text: str, pattern: str) -> int: """ The Knuth-Morris-Pratt Algorithm for finding a pattern within a piece of text with complexity O(n + m) @@ -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 j + return i-j j += 1 # if this is a prefix in our pattern @@ -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) + assert knuth_morris_pratt(text1, pattern) and not knuth_morris_pratt(text2, pattern) # Test 2) pattern = "ABABX" text = "ABABZABABYABABX" - assert kmp(pattern, text) + assert knuth_morris_pratt(text, pattern) # Test 3) pattern = "AAAB" text = "ABAAAAAB" - assert kmp(pattern, text) + assert knuth_morris_pratt(text, pattern) # Test 4) pattern = "abcdabcy" text = "abcxabcdabxabcdabcdabcy" - assert kmp(pattern, text) + assert knuth_morris_pratt(text, pattern) # Test 5) pattern = "aabaabaaa" From 448f5b55ac5e187c6877a31044dfe6220f8efbee Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sun, 24 Sep 2023 19:36:55 +0000 Subject: [PATCH 03/12] [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 6894cf43e18a..756844f1c5da 100644 --- a/strings/knuth_morris_pratt.py +++ b/strings/knuth_morris_pratt.py @@ -24,7 +24,7 @@ def knuth_morris_pratt(text: str, pattern: str) -> int: 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 From 4d7da6330ca0ed07e34c2ac7cf86c85318e0b5c0 Mon Sep 17 00:00:00 2001 From: Hetarth Jain Date: Mon, 25 Sep 2023 19:53:06 +0530 Subject: [PATCH 04/12] Update knuth_morris_pratt.py --- strings/knuth_morris_pratt.py | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/strings/knuth_morris_pratt.py b/strings/knuth_morris_pratt.py index 756844f1c5da..794574623cf1 100644 --- a/strings/knuth_morris_pratt.py +++ b/strings/knuth_morris_pratt.py @@ -61,7 +61,7 @@ def get_failure_array(pattern: str) -> list[int]: pattern = "abc1abc12" text1 = "alskfjaldsabc1abc1abc12k23adsfabcabc" text2 = "alskfjaldsk23adsfabcabc" - assert knuth_morris_pratt(text1, pattern) and not knuth_morris_pratt(text2, pattern) + assert knuth_morris_pratt(text1, pattern) and knuth_morris_pratt(text2, pattern) # Test 2) pattern = "ABABX" @@ -78,6 +78,11 @@ def get_failure_array(pattern: str) -> list[int]: text = "abcxabcdabxabcdabcdabcy" assert knuth_morris_pratt(text, pattern) - # Test 5) + # Test 5) -> Doctest + pattern = "kn" + text = "knuthmorrispratt" + print( text.find("kn") == knuth_morris_pratt(text, pattern) ) + + # Test 6) pattern = "aabaabaaa" assert get_failure_array(pattern) == [0, 1, 0, 1, 2, 3, 4, 5, 2] From 07b6240096dac09cf04c1ce405b0109e11de4b6c Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Mon, 25 Sep 2023 14:26:07 +0000 Subject: [PATCH 05/12] [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 794574623cf1..0a6e71d9076a 100644 --- a/strings/knuth_morris_pratt.py +++ b/strings/knuth_morris_pratt.py @@ -81,7 +81,7 @@ def get_failure_array(pattern: str) -> list[int]: # Test 5) -> Doctest pattern = "kn" text = "knuthmorrispratt" - print( text.find("kn") == knuth_morris_pratt(text, pattern) ) + print(text.find("kn") == knuth_morris_pratt(text, pattern)) # Test 6) pattern = "aabaabaaa" From 73fb0c5a514cd0bfe4373030d57887f6b4dd5a80 Mon Sep 17 00:00:00 2001 From: Hetarth Jain Date: Tue, 26 Sep 2023 00:17:48 +0530 Subject: [PATCH 06/12] Update knuth_morris_pratt.py --- strings/knuth_morris_pratt.py | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/strings/knuth_morris_pratt.py b/strings/knuth_morris_pratt.py index 0a6e71d9076a..5b41a1ea676a 100644 --- a/strings/knuth_morris_pratt.py +++ b/strings/knuth_morris_pratt.py @@ -78,10 +78,14 @@ def get_failure_array(pattern: str) -> list[int]: text = "abcxabcdabxabcdabcdabcy" assert knuth_morris_pratt(text, pattern) - # Test 5) -> Doctest - pattern = "kn" - text = "knuthmorrispratt" - print(text.find("kn") == knuth_morris_pratt(text, pattern)) + # Test 5) -> Doctests + kmp = "knuth_morris_pratt" + assert (knuth_morris_pratt(kmp, "kn") == kmp.find("kn")) + assert (knuth_morris_pratt(kmp, "h_m") == kmp.find("h_m")) + assert (knuth_morris_pratt(kmp, "rr") == kmp.find("rr")) + assert (knuth_morris_pratt(kmp, "tt") == kmp.find("tt")) + assert (knuth_morris_pratt(kmp, "not there") == kmp.find("not there")) + assert (all( knuth_morris_pratt(kmp, s) == kmp.find(s) for s in ( "kn", "h_m", "rr", "tt", "not there" ) )) # Test 6) pattern = "aabaabaaa" From 56b36a316e3eac66351a7f960eb85f9f2f72c4bf Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Mon, 25 Sep 2023 18:49:18 +0000 Subject: [PATCH 07/12] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- strings/knuth_morris_pratt.py | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/strings/knuth_morris_pratt.py b/strings/knuth_morris_pratt.py index 5b41a1ea676a..727ea4badd52 100644 --- a/strings/knuth_morris_pratt.py +++ b/strings/knuth_morris_pratt.py @@ -80,12 +80,15 @@ def get_failure_array(pattern: str) -> list[int]: # Test 5) -> Doctests kmp = "knuth_morris_pratt" - assert (knuth_morris_pratt(kmp, "kn") == kmp.find("kn")) - assert (knuth_morris_pratt(kmp, "h_m") == kmp.find("h_m")) - assert (knuth_morris_pratt(kmp, "rr") == kmp.find("rr")) - assert (knuth_morris_pratt(kmp, "tt") == kmp.find("tt")) - assert (knuth_morris_pratt(kmp, "not there") == kmp.find("not there")) - assert (all( knuth_morris_pratt(kmp, s) == kmp.find(s) for s in ( "kn", "h_m", "rr", "tt", "not there" ) )) + assert knuth_morris_pratt(kmp, "kn") == kmp.find("kn") + assert knuth_morris_pratt(kmp, "h_m") == kmp.find("h_m") + assert knuth_morris_pratt(kmp, "rr") == kmp.find("rr") + assert knuth_morris_pratt(kmp, "tt") == kmp.find("tt") + assert knuth_morris_pratt(kmp, "not there") == kmp.find("not there") + assert all( + knuth_morris_pratt(kmp, s) == kmp.find(s) + for s in ("kn", "h_m", "rr", "tt", "not there") + ) # Test 6) pattern = "aabaabaaa" From 67af28f66eff30c3e7ec64a54e8c7d9e1bda9b4e Mon Sep 17 00:00:00 2001 From: Hetarth Jain Date: Thu, 28 Sep 2023 14:38:24 +0530 Subject: [PATCH 08/12] Update back_propagation_neural_network.py --- neural_network/back_propagation_neural_network.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/neural_network/back_propagation_neural_network.py b/neural_network/back_propagation_neural_network.py index bdd096b3f653..5c741b4208c6 100644 --- a/neural_network/back_propagation_neural_network.py +++ b/neural_network/back_propagation_neural_network.py @@ -163,7 +163,7 @@ def cal_loss(self, ydata, ydata_): def plot_loss(self): if self.ax_loss.lines: - self.ax_loss.lines.remove(self.ax_loss.lines[0]) + self.ax_loss.lines[0].remove() self.ax_loss.plot(self.train_mse, "r-") plt.ion() plt.xlabel("step") From 6bfc22ce135015c8430efb969e2c0bdad78c4955 Mon Sep 17 00:00:00 2001 From: Hetarth Jain Date: Thu, 28 Sep 2023 15:04:09 +0530 Subject: [PATCH 09/12] Update back_propagation_neural_network.py --- neural_network/back_propagation_neural_network.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/neural_network/back_propagation_neural_network.py b/neural_network/back_propagation_neural_network.py index 5c741b4208c6..bdd096b3f653 100644 --- a/neural_network/back_propagation_neural_network.py +++ b/neural_network/back_propagation_neural_network.py @@ -163,7 +163,7 @@ def cal_loss(self, ydata, ydata_): def plot_loss(self): if self.ax_loss.lines: - self.ax_loss.lines[0].remove() + self.ax_loss.lines.remove(self.ax_loss.lines[0]) self.ax_loss.plot(self.train_mse, "r-") plt.ion() plt.xlabel("step") From eb360f781e391e1e8871d43289851779320cc70f Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Thu, 28 Sep 2023 20:10:35 +0200 Subject: [PATCH 10/12] Update strings/knuth_morris_pratt.py --- strings/knuth_morris_pratt.py | 5 ----- 1 file changed, 5 deletions(-) diff --git a/strings/knuth_morris_pratt.py b/strings/knuth_morris_pratt.py index 727ea4badd52..517d15b96026 100644 --- a/strings/knuth_morris_pratt.py +++ b/strings/knuth_morris_pratt.py @@ -80,11 +80,6 @@ def get_failure_array(pattern: str) -> list[int]: # Test 5) -> Doctests kmp = "knuth_morris_pratt" - assert knuth_morris_pratt(kmp, "kn") == kmp.find("kn") - assert knuth_morris_pratt(kmp, "h_m") == kmp.find("h_m") - assert knuth_morris_pratt(kmp, "rr") == kmp.find("rr") - assert knuth_morris_pratt(kmp, "tt") == kmp.find("tt") - assert knuth_morris_pratt(kmp, "not there") == kmp.find("not there") assert all( knuth_morris_pratt(kmp, s) == kmp.find(s) for s in ("kn", "h_m", "rr", "tt", "not there") From 462fa30fe83ed96b83f8175705be8b35ae2d4d1c Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Thu, 28 Sep 2023 20:17:40 +0200 Subject: [PATCH 11/12] Update knuth_morris_pratt.py --- strings/knuth_morris_pratt.py | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/strings/knuth_morris_pratt.py b/strings/knuth_morris_pratt.py index 517d15b96026..bad7c5d77b11 100644 --- a/strings/knuth_morris_pratt.py +++ b/strings/knuth_morris_pratt.py @@ -14,6 +14,12 @@ def knuth_morris_pratt(text: str, pattern: 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 + >>> knuth_morris_pratt = "knuth_morris_pratt" + >>> 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 @@ -57,6 +63,10 @@ def get_failure_array(pattern: str) -> list[int]: if __name__ == "__main__": + import doctest + + doctest.testmod() + # Test 1) pattern = "abc1abc12" text1 = "alskfjaldsabc1abc1abc12k23adsfabcabc" From c44b30ba8734e23611af1f9ee70191ed9dd9363d Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Thu, 28 Sep 2023 20:18:38 +0200 Subject: [PATCH 12/12] Update knuth_morris_pratt.py --- 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 bad7c5d77b11..8a04eb2532c0 100644 --- a/strings/knuth_morris_pratt.py +++ b/strings/knuth_morris_pratt.py @@ -14,7 +14,7 @@ def knuth_morris_pratt(text: str, pattern: 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 - >>> knuth_morris_pratt = "knuth_morris_pratt" + >>> kmp = "knuth_morris_pratt" >>> all( ... knuth_morris_pratt(kmp, s) == kmp.find(s) ... for s in ("kn", "h_m", "rr", "tt", "not there")