1
1
"""
2
2
Author : Alexander Pantyukhin
3
3
Date : November 3, 2022
4
+
4
5
Implement the class of prefix sum with useful functions based on it.
6
+
5
7
"""
6
8
7
9
@@ -18,8 +20,20 @@ def __init__(self, array: list[int]) -> None:
18
20
19
21
def get_sum (self , start : int , end : int ) -> int :
20
22
"""
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
23
37
"""
24
38
if not self .prefix_sum :
25
39
raise ValueError ("The array is empty." )
@@ -37,8 +51,10 @@ def contains_sum(self, target_sum: int) -> bool:
37
51
"""
38
52
The function returns True if array contains the target_sum,
39
53
False otherwise.
54
+
40
55
Runtime : O(n)
41
56
Space: O(n)
57
+
42
58
>>> PrefixSum([1,2,3]).contains_sum(6)
43
59
True
44
60
>>> PrefixSum([1,2,3]).contains_sum(5)
0 commit comments