From 0f2618bfbe0e8aabb6849d37a30b2c74f18d09b7 Mon Sep 17 00:00:00 2001 From: Prasad Ashok Chavan Date: Tue, 26 Sep 2023 09:03:34 +0530 Subject: [PATCH 01/12] polynomialHash --- hashes/polynomial_rolling_hash.py | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 hashes/polynomial_rolling_hash.py diff --git a/hashes/polynomial_rolling_hash.py b/hashes/polynomial_rolling_hash.py new file mode 100644 index 000000000000..a1d90caf7c9c --- /dev/null +++ b/hashes/polynomial_rolling_hash.py @@ -0,0 +1,30 @@ +''' +Polynomial Rolling Hash Algo --> +Polynomial rolling hash function is a hash function that uses only multiplications and additions. + +Time Complexity: O(N) + +Auxiliary Space: O(1) + +''' + +class Hash: + def __init__(self, s: str): + self.hash_value = 0 + self.p, self.m = 31, 10**9 + 7 + self.length = len(s) + hash_so_far = 0 + p_pow = 1 + for i in range(self.length): + hash_so_far = (hash_so_far + (1 + ord(s[i]) - ord('a')) * p_pow) % self.m + p_pow = (p_pow * self.p) % self.m + self.hash_value = hash_so_far + + def __eq__(self, other): + return self.hash_value == other.hash_value + + +if __name__ == '__main__': + s = "thealgorithms_python" + h = Hash(s) + print("Hash value of {} is {}".format(s, h.hash_value)) From eb5a78c460412c250fa43f32451a71d19753d95b Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Tue, 26 Sep 2023 03:37:38 +0000 Subject: [PATCH 02/12] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- hashes/polynomial_rolling_hash.py | 43 ++++++++++++++++--------------- 1 file changed, 22 insertions(+), 21 deletions(-) diff --git a/hashes/polynomial_rolling_hash.py b/hashes/polynomial_rolling_hash.py index a1d90caf7c9c..836ac3704fd0 100644 --- a/hashes/polynomial_rolling_hash.py +++ b/hashes/polynomial_rolling_hash.py @@ -1,4 +1,4 @@ -''' +""" Polynomial Rolling Hash Algo --> Polynomial rolling hash function is a hash function that uses only multiplications and additions. @@ -6,25 +6,26 @@ Auxiliary Space: O(1) -''' +""" + class Hash: - def __init__(self, s: str): - self.hash_value = 0 - self.p, self.m = 31, 10**9 + 7 - self.length = len(s) - hash_so_far = 0 - p_pow = 1 - for i in range(self.length): - hash_so_far = (hash_so_far + (1 + ord(s[i]) - ord('a')) * p_pow) % self.m - p_pow = (p_pow * self.p) % self.m - self.hash_value = hash_so_far - - def __eq__(self, other): - return self.hash_value == other.hash_value - - -if __name__ == '__main__': - s = "thealgorithms_python" - h = Hash(s) - print("Hash value of {} is {}".format(s, h.hash_value)) + def __init__(self, s: str): + self.hash_value = 0 + self.p, self.m = 31, 10**9 + 7 + self.length = len(s) + hash_so_far = 0 + p_pow = 1 + for i in range(self.length): + hash_so_far = (hash_so_far + (1 + ord(s[i]) - ord("a")) * p_pow) % self.m + p_pow = (p_pow * self.p) % self.m + self.hash_value = hash_so_far + + def __eq__(self, other): + return self.hash_value == other.hash_value + + +if __name__ == "__main__": + s = "thealgorithms_python" + h = Hash(s) + print("Hash value of {} is {}".format(s, h.hash_value)) From a5e27f29153ba8990a15ba846b4cbc456956f377 Mon Sep 17 00:00:00 2001 From: Prasad Ashok Chavan <97186762+prasad-chavan1@users.noreply.github.com> Date: Tue, 26 Sep 2023 14:36:50 +0530 Subject: [PATCH 03/12] Update polynomial_rolling_hash.py --- hashes/polynomial_rolling_hash.py | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/hashes/polynomial_rolling_hash.py b/hashes/polynomial_rolling_hash.py index 836ac3704fd0..0094b84a2434 100644 --- a/hashes/polynomial_rolling_hash.py +++ b/hashes/polynomial_rolling_hash.py @@ -6,26 +6,28 @@ Auxiliary Space: O(1) -""" - +Wikipedia link: https://en.wikipedia.org/wiki/Rolling_hash +""" class Hash: - def __init__(self, s: str): + + def __init__(self, s=str): self.hash_value = 0 - self.p, self.m = 31, 10**9 + 7 + (self.p, self.m) = (31, 10 ** 9 + 7) self.length = len(s) hash_so_far = 0 p_pow = 1 for i in range(self.length): - hash_so_far = (hash_so_far + (1 + ord(s[i]) - ord("a")) * p_pow) % self.m - p_pow = (p_pow * self.p) % self.m + hash_so_far = (hash_so_far + (1 + ord(s[i]) - ord('a')) + * p_pow) % self.m + p_pow = p_pow * self.p % self.m self.hash_value = hash_so_far def __eq__(self, other): return self.hash_value == other.hash_value -if __name__ == "__main__": - s = "thealgorithms_python" +if __name__ == '__main__': + s = 'thealgorithms_python' h = Hash(s) - print("Hash value of {} is {}".format(s, h.hash_value)) + print('The hash value of '+ s +' is '+ h.hash_value) From 9963b4309ca90f42cc1f81c3ebdf9483668a19a8 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Tue, 26 Sep 2023 09:08:05 +0000 Subject: [PATCH 04/12] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- hashes/polynomial_rolling_hash.py | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/hashes/polynomial_rolling_hash.py b/hashes/polynomial_rolling_hash.py index 0094b84a2434..5517f5677342 100644 --- a/hashes/polynomial_rolling_hash.py +++ b/hashes/polynomial_rolling_hash.py @@ -9,17 +9,17 @@ Wikipedia link: https://en.wikipedia.org/wiki/Rolling_hash """ -class Hash: + +class Hash: def __init__(self, s=str): self.hash_value = 0 - (self.p, self.m) = (31, 10 ** 9 + 7) + (self.p, self.m) = (31, 10**9 + 7) self.length = len(s) hash_so_far = 0 p_pow = 1 for i in range(self.length): - hash_so_far = (hash_so_far + (1 + ord(s[i]) - ord('a')) - * p_pow) % self.m + hash_so_far = (hash_so_far + (1 + ord(s[i]) - ord("a")) * p_pow) % self.m p_pow = p_pow * self.p % self.m self.hash_value = hash_so_far @@ -27,7 +27,7 @@ def __eq__(self, other): return self.hash_value == other.hash_value -if __name__ == '__main__': - s = 'thealgorithms_python' +if __name__ == "__main__": + s = "thealgorithms_python" h = Hash(s) - print('The hash value of '+ s +' is '+ h.hash_value) + print("The hash value of " + s + " is " + h.hash_value) From 26ff44ff712a7dd2ed587f943980412207921a33 Mon Sep 17 00:00:00 2001 From: Prasad Ashok Chavan <97186762+prasad-chavan1@users.noreply.github.com> Date: Tue, 26 Sep 2023 22:39:38 +0530 Subject: [PATCH 05/12] Update polynomial_rolling_hash.py --- hashes/polynomial_rolling_hash.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/hashes/polynomial_rolling_hash.py b/hashes/polynomial_rolling_hash.py index 5517f5677342..502e82c4ea1b 100644 --- a/hashes/polynomial_rolling_hash.py +++ b/hashes/polynomial_rolling_hash.py @@ -1,6 +1,7 @@ """ Polynomial Rolling Hash Algo --> -Polynomial rolling hash function is a hash function that uses only multiplications and additions. +Polynomial rolling hash function is hash +function that uses only multiplications and additions Time Complexity: O(N) From 63e9a43de5ee9447e3a8c41cfe6574f7d617017a Mon Sep 17 00:00:00 2001 From: Prasad Ashok Chavan <97186762+prasad-chavan1@users.noreply.github.com> Date: Tue, 26 Sep 2023 22:47:00 +0530 Subject: [PATCH 06/12] Update polynomial_rolling_hash.py --- hashes/polynomial_rolling_hash.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/hashes/polynomial_rolling_hash.py b/hashes/polynomial_rolling_hash.py index 502e82c4ea1b..72d1096ccef1 100644 --- a/hashes/polynomial_rolling_hash.py +++ b/hashes/polynomial_rolling_hash.py @@ -13,7 +13,7 @@ class Hash: - def __init__(self, s=str): + def __init__(self, s=str) ->None: self.hash_value = 0 (self.p, self.m) = (31, 10**9 + 7) self.length = len(s) From 8475bc1af6dadc2eadc90a89ec6e39f3b0e1e9c5 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Tue, 26 Sep 2023 17:21:08 +0000 Subject: [PATCH 07/12] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- hashes/polynomial_rolling_hash.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/hashes/polynomial_rolling_hash.py b/hashes/polynomial_rolling_hash.py index 72d1096ccef1..e9ee0b4cabdf 100644 --- a/hashes/polynomial_rolling_hash.py +++ b/hashes/polynomial_rolling_hash.py @@ -13,7 +13,7 @@ class Hash: - def __init__(self, s=str) ->None: + def __init__(self, s=str) -> None: self.hash_value = 0 (self.p, self.m) = (31, 10**9 + 7) self.length = len(s) From 86075a56930449a05bdc1c8cdce8c3c07cf9f2fe Mon Sep 17 00:00:00 2001 From: Prasad Ashok Chavan Date: Wed, 27 Sep 2023 08:05:18 +0530 Subject: [PATCH 08/12] updated --- hashes/polynomial_rolling_hash.py | 44 ++++++++++++++++++------------- 1 file changed, 25 insertions(+), 19 deletions(-) diff --git a/hashes/polynomial_rolling_hash.py b/hashes/polynomial_rolling_hash.py index a1d90caf7c9c..545b6d9557b6 100644 --- a/hashes/polynomial_rolling_hash.py +++ b/hashes/polynomial_rolling_hash.py @@ -1,30 +1,36 @@ -''' +""" Polynomial Rolling Hash Algo --> -Polynomial rolling hash function is a hash function that uses only multiplications and additions. +Polynomial rolling hash function is a hash +function that uses only multiplications and additions. Time Complexity: O(N) Auxiliary Space: O(1) -''' +Wikipedia link: https://en.wikipedia.org/wiki/Rolling_hash + +""" + class Hash: - def __init__(self, s: str): - self.hash_value = 0 - self.p, self.m = 31, 10**9 + 7 - self.length = len(s) - hash_so_far = 0 - p_pow = 1 - for i in range(self.length): - hash_so_far = (hash_so_far + (1 + ord(s[i]) - ord('a')) * p_pow) % self.m - p_pow = (p_pow * self.p) % self.m - self.hash_value = hash_so_far - - def __eq__(self, other): - return self.hash_value == other.hash_value + + def __init__(self, input_str=str) ->None: + self.hash_value = 0 + (self.p, self.m) = (31, 10 ** 9 + 7) + self.length = len(input_str) + hash_so_far = 0 + p_pow = 1 + for i in range(self.length): + hash_so_far = (hash_so_far + (1 + ord(input_str[i]) - ord('a')) + * p_pow) % self.m + p_pow = p_pow * self.p % self.m + self.hash_value = hash_so_far + + def __eq__(self, other): + return self.hash_value == other.hash_value if __name__ == '__main__': - s = "thealgorithms_python" - h = Hash(s) - print("Hash value of {} is {}".format(s, h.hash_value)) + s = 'thealgorithms_python' + h = Hash(s) + print('The hash value of '+ s +' is '+ h.hash_value) From 23d758aa74f770b6f4ca34444f85007a81efa98d Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Wed, 27 Sep 2023 02:37:03 +0000 Subject: [PATCH 09/12] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- hashes/polynomial_rolling_hash.py | 38 +++++++++++++++---------------- 1 file changed, 19 insertions(+), 19 deletions(-) diff --git a/hashes/polynomial_rolling_hash.py b/hashes/polynomial_rolling_hash.py index 1735deeeb382..333a18438f03 100644 --- a/hashes/polynomial_rolling_hash.py +++ b/hashes/polynomial_rolling_hash.py @@ -12,22 +12,22 @@ class Hash: - def __init__(self, s: str): - self.hash_value = 0 - self.p, self.m = 31, 10**9 + 7 - self.length = len(s) - hash_so_far = 0 - p_pow = 1 - for i in range(self.length): - hash_so_far = (hash_so_far + (1 + ord(s[i]) - ord('a')) * p_pow) % self.m - p_pow = (p_pow * self.p) % self.m - self.hash_value = hash_so_far - - def __eq__(self, other): - return self.hash_value == other.hash_value - - -if __name__ == '__main__': - s = "thealgorithms_python" - h = Hash(s) - print("Hash value of {} is {}".format(s, h.hash_value)) + def __init__(self, s: str): + self.hash_value = 0 + self.p, self.m = 31, 10**9 + 7 + self.length = len(s) + hash_so_far = 0 + p_pow = 1 + for i in range(self.length): + hash_so_far = (hash_so_far + (1 + ord(s[i]) - ord("a")) * p_pow) % self.m + p_pow = (p_pow * self.p) % self.m + self.hash_value = hash_so_far + + def __eq__(self, other): + return self.hash_value == other.hash_value + + +if __name__ == "__main__": + s = "thealgorithms_python" + h = Hash(s) + print("Hash value of {} is {}".format(s, h.hash_value)) From d699d85c2fdc5b2b343311c95888b51b5ca89a4e Mon Sep 17 00:00:00 2001 From: Prasad Ashok Chavan <97186762+prasad-chavan1@users.noreply.github.com> Date: Wed, 27 Sep 2023 08:10:42 +0530 Subject: [PATCH 10/12] Update polynomial_rolling_hash.py --- hashes/polynomial_rolling_hash.py | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/hashes/polynomial_rolling_hash.py b/hashes/polynomial_rolling_hash.py index 333a18438f03..788365eca2d0 100644 --- a/hashes/polynomial_rolling_hash.py +++ b/hashes/polynomial_rolling_hash.py @@ -1,6 +1,7 @@ """ Polynomial Rolling Hash Algo --> -Polynomial rolling hash function is a hash function that uses only multiplications and additions. +Polynomial rolling hash function is a hash +function that uses only multiplications and additions. Time Complexity: O(N) @@ -12,14 +13,14 @@ class Hash: - def __init__(self, s: str): + def __init__(self, input_str: str) ->None: self.hash_value = 0 self.p, self.m = 31, 10**9 + 7 - self.length = len(s) + self.length = len(input_str) hash_so_far = 0 p_pow = 1 for i in range(self.length): - hash_so_far = (hash_so_far + (1 + ord(s[i]) - ord("a")) * p_pow) % self.m + hash_so_far = (hash_so_far + (1 + ord(input_str[i]) - ord("a")) * p_pow) % self.m p_pow = (p_pow * self.p) % self.m self.hash_value = hash_so_far From 4e07fbd96f29fffc58be86050c4fc42d0a307d4d Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Wed, 27 Sep 2023 02:41:17 +0000 Subject: [PATCH 11/12] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- hashes/polynomial_rolling_hash.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/hashes/polynomial_rolling_hash.py b/hashes/polynomial_rolling_hash.py index 788365eca2d0..c3dfc1bd872f 100644 --- a/hashes/polynomial_rolling_hash.py +++ b/hashes/polynomial_rolling_hash.py @@ -1,6 +1,6 @@ """ Polynomial Rolling Hash Algo --> -Polynomial rolling hash function is a hash +Polynomial rolling hash function is a hash function that uses only multiplications and additions. Time Complexity: O(N) @@ -13,14 +13,16 @@ class Hash: - def __init__(self, input_str: str) ->None: + def __init__(self, input_str: str) -> None: self.hash_value = 0 self.p, self.m = 31, 10**9 + 7 self.length = len(input_str) hash_so_far = 0 p_pow = 1 for i in range(self.length): - hash_so_far = (hash_so_far + (1 + ord(input_str[i]) - ord("a")) * p_pow) % self.m + hash_so_far = ( + hash_so_far + (1 + ord(input_str[i]) - ord("a")) * p_pow + ) % self.m p_pow = (p_pow * self.p) % self.m self.hash_value = hash_so_far From 2837fd96eed288a39fb8c3456aa6343305d86ab8 Mon Sep 17 00:00:00 2001 From: Prasad Ashok Chavan <97186762+prasad-chavan1@users.noreply.github.com> Date: Wed, 27 Sep 2023 08:14:26 +0530 Subject: [PATCH 12/12] Update polynomial_rolling_hash.py --- hashes/polynomial_rolling_hash.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/hashes/polynomial_rolling_hash.py b/hashes/polynomial_rolling_hash.py index c3dfc1bd872f..78d0ed3c97e5 100644 --- a/hashes/polynomial_rolling_hash.py +++ b/hashes/polynomial_rolling_hash.py @@ -33,4 +33,4 @@ def __eq__(self, other): if __name__ == "__main__": s = "thealgorithms_python" h = Hash(s) - print("Hash value of {} is {}".format(s, h.hash_value)) + print(f"Hash value of {s} is {h.hash_value}")