Largest Continuous Sum of Elements in a List - Python
def large_cont_sum(l):if len(l)==0:
return 0
max_sum = current_sum = l[0]
for num in l[1:]:
current_sum = max(current_sum+num, num)
max_sum = max(current_sum, max_sum)
return max_sum
Test
large_cont_sum([1,2,-1,-10,3,4,10,-2,-1])%timeit large_cont_sum([1,2,-1,-10,3,4,10,-2,-1])
Output
17100000 loops, best of 3: 3.66 µs per loop
Comments
Post a Comment