Skip to content

Commit 6c680b1

Browse files
Update prefix_sum.py
1 parent b0d0021 commit 6c680b1

File tree

1 file changed

+18
-2
lines changed

1 file changed

+18
-2
lines changed

data_structures/arrays/prefix_sum.py

+18-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
"""
22
Author : Alexander Pantyukhin
33
Date : November 3, 2022
4+
45
Implement the class of prefix sum with useful functions based on it.
6+
57
"""
68

79

@@ -18,8 +20,20 @@ def __init__(self, array: list[int]) -> None:
1820

1921
def get_sum(self, start: int, end: int) -> int:
2022
"""
21-
Returns the sum of the array from index start to end (inclusive).
22-
Raises ValueError if the indices are invalid.
23+
The function returns the sum of array from the start to the end indexes
24+
Runtime : O(1)
25+
Space: O(1)
26+
27+
>>> PrefixSum([1,2,3]).get_sum(0, 2)
28+
6
29+
>>> PrefixSum([1,2,3]).get_sum(1, 2)
30+
5
31+
>>> PrefixSum([1,2,3]).get_sum(2, 2)
32+
3
33+
>>> PrefixSum([1,2,3]).get_sum(2, 3)
34+
Traceback (most recent call last):
35+
...
36+
IndexError: list index out of range
2337
"""
2438
if not self.prefix_sum:
2539
raise ValueError("The array is empty.")
@@ -37,8 +51,10 @@ def contains_sum(self, target_sum: int) -> bool:
3751
"""
3852
The function returns True if array contains the target_sum,
3953
False otherwise.
54+
4055
Runtime : O(n)
4156
Space: O(n)
57+
4258
>>> PrefixSum([1,2,3]).contains_sum(6)
4359
True
4460
>>> PrefixSum([1,2,3]).contains_sum(5)

0 commit comments

Comments
 (0)