From cae97f0dfa44bbd5095880a9f0c52a8825effcff Mon Sep 17 00:00:00 2001 From: Pranjay kumar <110048711+pranjaykumar926@users.noreply.github.com> Date: Wed, 5 Feb 2025 00:09:35 +0530 Subject: [PATCH 1/7] Update prefix_sum.py MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Index Validation for get_sum Raises ValueError if start or end is out of range or start > end. Handles cases where the array is empty. ✅ Empty Array Support If an empty array is passed, get_sum raises an appropriate error instead of failing unexpectedly. ✅ Optimized contains_sum Initialization Initializes sums with {0} for efficient subarray sum checking. --- data_structures/arrays/prefix_sum.py | 60 +++++----------------------- 1 file changed, 11 insertions(+), 49 deletions(-) diff --git a/data_structures/arrays/prefix_sum.py b/data_structures/arrays/prefix_sum.py index 2243a5308937..8d65f3fff13d 100644 --- a/data_structures/arrays/prefix_sum.py +++ b/data_structures/arrays/prefix_sum.py @@ -1,12 +1,3 @@ -""" -Author : Alexander Pantyukhin -Date : November 3, 2022 - -Implement the class of prefix sum with useful functions based on it. - -""" - - class PrefixSum: def __init__(self, array: list[int]) -> None: len_array = len(array) @@ -20,53 +11,25 @@ def __init__(self, array: list[int]) -> None: def get_sum(self, start: int, end: int) -> int: """ - The function returns the sum of array from the start to the end indexes. - Runtime : O(1) - Space: O(1) - - >>> PrefixSum([1,2,3]).get_sum(0, 2) - 6 - >>> PrefixSum([1,2,3]).get_sum(1, 2) - 5 - >>> PrefixSum([1,2,3]).get_sum(2, 2) - 3 - >>> PrefixSum([1,2,3]).get_sum(2, 3) - Traceback (most recent call last): - ... - IndexError: list index out of range + Returns the sum of the array from index start to end (inclusive). + Raises ValueError if the indices are invalid. """ - if start == 0: - return self.prefix_sum[end] + if not self.prefix_sum: + raise ValueError("The array is empty.") + + if start < 0 or end >= len(self.prefix_sum) or start > end: + raise ValueError("Invalid range specified.") - return self.prefix_sum[end] - self.prefix_sum[start - 1] + return self.prefix_sum[end] if start == 0 else self.prefix_sum[end] - self.prefix_sum[start - 1] def contains_sum(self, target_sum: int) -> bool: """ - The function returns True if array contains the target_sum, - False otherwise. - - Runtime : O(n) - Space: O(n) - - >>> PrefixSum([1,2,3]).contains_sum(6) - True - >>> PrefixSum([1,2,3]).contains_sum(5) - True - >>> PrefixSum([1,2,3]).contains_sum(3) - True - >>> PrefixSum([1,2,3]).contains_sum(4) - False - >>> PrefixSum([1,2,3]).contains_sum(7) - False - >>> PrefixSum([1,-2,3]).contains_sum(2) - True + Returns True if any subarray sum equals target_sum, otherwise False. """ - - sums = {0} + sums = {0} # Initialize with 0 to check subarrays from the start for sum_item in self.prefix_sum: - if sum_item - target_sum in sums: + if (sum_item - target_sum) in sums: return True - sums.add(sum_item) return False @@ -74,5 +37,4 @@ def contains_sum(self, target_sum: int) -> bool: if __name__ == "__main__": import doctest - doctest.testmod() From c8a345ead1f7e777b58b2a8b24ade04699de08fb Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Tue, 4 Feb 2025 18:40:48 +0000 Subject: [PATCH 2/7] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- data_structures/arrays/prefix_sum.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/data_structures/arrays/prefix_sum.py b/data_structures/arrays/prefix_sum.py index 8d65f3fff13d..12659a57aa04 100644 --- a/data_structures/arrays/prefix_sum.py +++ b/data_structures/arrays/prefix_sum.py @@ -20,7 +20,11 @@ def get_sum(self, start: int, end: int) -> int: if start < 0 or end >= len(self.prefix_sum) or start > end: raise ValueError("Invalid range specified.") - return self.prefix_sum[end] if start == 0 else self.prefix_sum[end] - self.prefix_sum[start - 1] + return ( + self.prefix_sum[end] + if start == 0 + else self.prefix_sum[end] - self.prefix_sum[start - 1] + ) def contains_sum(self, target_sum: int) -> bool: """ @@ -37,4 +41,5 @@ def contains_sum(self, target_sum: int) -> bool: if __name__ == "__main__": import doctest + doctest.testmod() From b0d00214d77b1766bafc94260866c396069b0c9f Mon Sep 17 00:00:00 2001 From: Maxim Smolskiy Date: Thu, 20 Mar 2025 02:22:56 +0300 Subject: [PATCH 3/7] Update prefix_sum.py --- data_structures/arrays/prefix_sum.py | 30 +++++++++++++++++++++++++--- 1 file changed, 27 insertions(+), 3 deletions(-) diff --git a/data_structures/arrays/prefix_sum.py b/data_structures/arrays/prefix_sum.py index 12659a57aa04..77ac7dd3f2dc 100644 --- a/data_structures/arrays/prefix_sum.py +++ b/data_structures/arrays/prefix_sum.py @@ -1,3 +1,10 @@ +""" +Author : Alexander Pantyukhin +Date : November 3, 2022 +Implement the class of prefix sum with useful functions based on it. +""" + + class PrefixSum: def __init__(self, array: list[int]) -> None: len_array = len(array) @@ -28,12 +35,29 @@ def get_sum(self, start: int, end: int) -> int: def contains_sum(self, target_sum: int) -> bool: """ - Returns True if any subarray sum equals target_sum, otherwise False. + The function returns True if array contains the target_sum, + False otherwise. + Runtime : O(n) + Space: O(n) + >>> PrefixSum([1,2,3]).contains_sum(6) + True + >>> PrefixSum([1,2,3]).contains_sum(5) + True + >>> PrefixSum([1,2,3]).contains_sum(3) + True + >>> PrefixSum([1,2,3]).contains_sum(4) + False + >>> PrefixSum([1,2,3]).contains_sum(7) + False + >>> PrefixSum([1,-2,3]).contains_sum(2) + True """ - sums = {0} # Initialize with 0 to check subarrays from the start + + sums = {0} for sum_item in self.prefix_sum: - if (sum_item - target_sum) in sums: + if sum_item - target_sum in sums: return True + sums.add(sum_item) return False From 6c680b1664096f3a6096582e0cad16efe4f71579 Mon Sep 17 00:00:00 2001 From: Maxim Smolskiy Date: Thu, 20 Mar 2025 02:24:29 +0300 Subject: [PATCH 4/7] Update prefix_sum.py --- data_structures/arrays/prefix_sum.py | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/data_structures/arrays/prefix_sum.py b/data_structures/arrays/prefix_sum.py index 77ac7dd3f2dc..fab7244dd320 100644 --- a/data_structures/arrays/prefix_sum.py +++ b/data_structures/arrays/prefix_sum.py @@ -1,7 +1,9 @@ """ Author : Alexander Pantyukhin Date : November 3, 2022 + Implement the class of prefix sum with useful functions based on it. + """ @@ -18,8 +20,20 @@ def __init__(self, array: list[int]) -> None: def get_sum(self, start: int, end: int) -> int: """ - Returns the sum of the array from index start to end (inclusive). - Raises ValueError if the indices are invalid. + The function returns the sum of array from the start to the end indexes + Runtime : O(1) + Space: O(1) + + >>> PrefixSum([1,2,3]).get_sum(0, 2) + 6 + >>> PrefixSum([1,2,3]).get_sum(1, 2) + 5 + >>> PrefixSum([1,2,3]).get_sum(2, 2) + 3 + >>> PrefixSum([1,2,3]).get_sum(2, 3) + Traceback (most recent call last): + ... + IndexError: list index out of range """ if not self.prefix_sum: raise ValueError("The array is empty.") @@ -37,8 +51,10 @@ def contains_sum(self, target_sum: int) -> bool: """ The function returns True if array contains the target_sum, False otherwise. + Runtime : O(n) Space: O(n) + >>> PrefixSum([1,2,3]).contains_sum(6) True >>> PrefixSum([1,2,3]).contains_sum(5) From 6bee841070386a7fdbe0b04ebffdd3290bff378b Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Wed, 19 Mar 2025 23:24:53 +0000 Subject: [PATCH 5/7] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- data_structures/arrays/prefix_sum.py | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/data_structures/arrays/prefix_sum.py b/data_structures/arrays/prefix_sum.py index fab7244dd320..f5a7c7a951e4 100644 --- a/data_structures/arrays/prefix_sum.py +++ b/data_structures/arrays/prefix_sum.py @@ -24,16 +24,16 @@ def get_sum(self, start: int, end: int) -> int: Runtime : O(1) Space: O(1) - >>> PrefixSum([1,2,3]).get_sum(0, 2) - 6 - >>> PrefixSum([1,2,3]).get_sum(1, 2) - 5 - >>> PrefixSum([1,2,3]).get_sum(2, 2) - 3 - >>> PrefixSum([1,2,3]).get_sum(2, 3) - Traceback (most recent call last): - ... - IndexError: list index out of range + >>> PrefixSum([1,2,3]).get_sum(0, 2) + 6 + >>> PrefixSum([1,2,3]).get_sum(1, 2) + 5 + >>> PrefixSum([1,2,3]).get_sum(2, 2) + 3 + >>> PrefixSum([1,2,3]).get_sum(2, 3) + Traceback (most recent call last): + ... + IndexError: list index out of range """ if not self.prefix_sum: raise ValueError("The array is empty.") From de5705c062a572db365e80516577c7bc83e3c847 Mon Sep 17 00:00:00 2001 From: Maxim Smolskiy Date: Thu, 20 Mar 2025 02:26:23 +0300 Subject: [PATCH 6/7] Update prefix_sum.py --- data_structures/arrays/prefix_sum.py | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/data_structures/arrays/prefix_sum.py b/data_structures/arrays/prefix_sum.py index f5a7c7a951e4..8221935f7e17 100644 --- a/data_structures/arrays/prefix_sum.py +++ b/data_structures/arrays/prefix_sum.py @@ -20,7 +20,7 @@ def __init__(self, array: list[int]) -> None: def get_sum(self, start: int, end: int) -> int: """ - The function returns the sum of array from the start to the end indexes + The function returns the sum of array from the start to the end indexes. Runtime : O(1) Space: O(1) @@ -41,11 +41,10 @@ def get_sum(self, start: int, end: int) -> int: if start < 0 or end >= len(self.prefix_sum) or start > end: raise ValueError("Invalid range specified.") - return ( - self.prefix_sum[end] - if start == 0 - else self.prefix_sum[end] - self.prefix_sum[start - 1] - ) + if start == 0: + return self.prefix_sum[end] + + return self.prefix_sum[end] - self.prefix_sum[start - 1] def contains_sum(self, target_sum: int) -> bool: """ From c697a4cbbe7bb62fe9bc20732b43acbebabf38a3 Mon Sep 17 00:00:00 2001 From: Maxim Smolskiy Date: Thu, 20 Mar 2025 02:30:18 +0300 Subject: [PATCH 7/7] Update prefix_sum.py --- data_structures/arrays/prefix_sum.py | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/data_structures/arrays/prefix_sum.py b/data_structures/arrays/prefix_sum.py index 8221935f7e17..717b5f9d7e7e 100644 --- a/data_structures/arrays/prefix_sum.py +++ b/data_structures/arrays/prefix_sum.py @@ -30,10 +30,22 @@ def get_sum(self, start: int, end: int) -> int: 5 >>> PrefixSum([1,2,3]).get_sum(2, 2) 3 + >>> PrefixSum([]).get_sum(0, 0) + Traceback (most recent call last): + ... + ValueError: The array is empty. + >>> PrefixSum([1,2,3]).get_sum(-1, 2) + Traceback (most recent call last): + ... + ValueError: Invalid range specified. >>> PrefixSum([1,2,3]).get_sum(2, 3) Traceback (most recent call last): ... - IndexError: list index out of range + ValueError: Invalid range specified. + >>> PrefixSum([1,2,3]).get_sum(2, 1) + Traceback (most recent call last): + ... + ValueError: Invalid range specified. """ if not self.prefix_sum: raise ValueError("The array is empty.")