Skip to content

Update prefix_sum.py #12138

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 13 additions & 9 deletions data_structures/arrays/prefix_sum.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,6 @@
"""
Author : Alexander Pantyukhin
Date : November 3, 2022

Implement the class of prefix sum with useful functions based on it.

Check failure on line 2 in data_structures/arrays/prefix_sum.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff

data_structures/arrays/prefix_sum.py:2:11: SyntaxError: Simple statements must be separated by newlines or semicolons

Check failure on line 2 in data_structures/arrays/prefix_sum.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff

data_structures/arrays/prefix_sum.py:2:15: SyntaxError: Compound statements are not allowed on the same line as simple statements

Check failure on line 2 in data_structures/arrays/prefix_sum.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff

data_structures/arrays/prefix_sum.py:2:24: SyntaxError: Expected ':', found name

Check failure on line 2 in data_structures/arrays/prefix_sum.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff

data_structures/arrays/prefix_sum.py:2:31: SyntaxError: Simple statements must be separated by newlines or semicolons

Check failure on line 2 in data_structures/arrays/prefix_sum.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff

data_structures/arrays/prefix_sum.py:2:35: SyntaxError: Compound statements are not allowed on the same line as simple statements

Check failure on line 2 in data_structures/arrays/prefix_sum.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff

data_structures/arrays/prefix_sum.py:2:47: SyntaxError: Expected ',', found name

Check failure on line 2 in data_structures/arrays/prefix_sum.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff

data_structures/arrays/prefix_sum.py:2:57: SyntaxError: Expected ',', found name

Check failure on line 2 in data_structures/arrays/prefix_sum.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff

data_structures/arrays/prefix_sum.py:2:63: SyntaxError: Expected ',', found name

Check failure on line 2 in data_structures/arrays/prefix_sum.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff

data_structures/arrays/prefix_sum.py:2:66: SyntaxError: Expected ',', found name

Check failure on line 3 in data_structures/arrays/prefix_sum.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff

data_structures/arrays/prefix_sum.py:2:69: SyntaxError: Expected an identifier
"""


class PrefixSum:
def __init__(self, array: list[int]) -> None:
len_array = len(array)
Expand All @@ -22,7 +16,7 @@
"""
The function returns the sum of array from the start to the end indexes.
Runtime : O(1)
Space: O(1)
Space: O(n) (due to the prefix sum array)

>>> PrefixSum([1,2,3]).get_sum(0, 2)
6
Expand All @@ -35,14 +29,17 @@
...
IndexError: list index out of range
"""
if start > end or end >= len(self.prefix_sum):
raise IndexError("Invalid range specified.")

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:
"""
The function returns True if array contains the target_sum,
The function returns True if any subarray contains the target_sum,
False otherwise.

Runtime : O(n)
Expand All @@ -64,7 +61,7 @@

sums = {0}
for sum_item in self.prefix_sum:
if sum_item - target_sum in sums:
if sum_item == target_sum or sum_item - target_sum in sums:
return True

sums.add(sum_item)
Expand All @@ -76,3 +73,10 @@
import doctest

doctest.testmod()







Loading