forked from ngiengkianyew/daily-coding-problem
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproblem_102.py
More file actions
24 lines (21 loc) · 694 Bytes
/
problem_102.py
File metadata and controls
24 lines (21 loc) · 694 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
def get_cont_arr(arr, target):
summed = 0
start, end = 0, 0
i = 0
while i < len(arr):
if summed == target:
return arr[start:end]
elif summed > target:
summed -= arr[start]
start += 1
else:
summed += arr[i]
end = i + 1
i += 1
assert get_cont_arr([1, 2, 3, 4, 5], 0) == []
assert get_cont_arr([1, 2, 3, 4, 5], 1) == [1]
assert get_cont_arr([1, 2, 3, 4, 5], 5) == [2, 3]
assert get_cont_arr([5, 4, 3, 4, 5], 12) == [5, 4, 3]
assert get_cont_arr([5, 4, 3, 4, 5], 11) == [4, 3, 4]
assert get_cont_arr([1, 2, 3, 4, 5], 9) == [2, 3, 4]
assert get_cont_arr([1, 2, 3, 4, 5], 3) == [1, 2]