-
Notifications
You must be signed in to change notification settings - Fork 9
/
ex_10_03.py
26 lines (22 loc) · 707 Bytes
/
ex_10_03.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
#!/usr/bin/python
#AUTHOR: alexxa
#DATE: 28.12.2013
#SOURCE: Think Python: How to Think Like a Computer Scientist by Allen B. Downey
# http://www.greenteapress.com/thinkpython/html/index.html
#PURPOSE: Chapter 10. Lists
# Exercise 10.3
# Write a function that takes a list of numbers and returns
# the cumulative sum; that is, a new list where the ith element
# is the sum of the first i + 1 elements from the original list.
# For example, the cumulative sum of [1, 2, 3] is [1, 3, 6].
def nested_sum(listik):
total = 0
res = []
for i in listik:
total += i
res.append(total)
return res
print(nested_sum([1,2,3]))
print(nested_sum([]))
print(nested_sum([-1,1,2,0]))
#END