-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathconsecutive.py
46 lines (35 loc) · 994 Bytes
/
consecutive.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# -*- coding: utf-8 -*-
'''
Given an array of positive integers (excluding zero) and a target number,
detect wheter ther is a set of consecutive elements in the array that add
to the target.
Example: a = {1, 3, 5, 7, 9}
target 8
output = true ({3,5})
'''
def consecutive(a, target):
if not a:
return False
#will be the index of the first non-removed element
low = 0
i = 0
s = 0
while i < len(a):
if s > target:
s-=a[low]
low+=1
continue
if s == target:
print a[low:i]
return True
s+=a[i]
i+=1
return s == target
assert consecutive([3, 7, 9, 15, 2, 1, 5], 8)
assert consecutive([1, 3, 4, 7, 9, 15, 2, 5], 7)
assert consecutive([5, 7, 3, 1, 4], 4)
assert consecutive([1,2,3,4,5], 7)
assert consecutive([100, 2, 3, 1], 5)
assert consecutive([1, 3, 5, 7, 9], 12)
assert consecutive([1, 3, 7], 11)
assert consecutive([1, 3, 5, 7, 9], 8)