-
Notifications
You must be signed in to change notification settings - Fork 9
/
ex_10_01.py
27 lines (23 loc) · 722 Bytes
/
ex_10_01.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
#!/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.1
# Write a function called nested_sum that takes a nested list
# of integers and add up the elements from all of the nested
# lists.
def nested_sum(listik):
total = 0
for i in listik:
if type(i) is list:
i = nested_sum(i)
total += i
return total
print(nested_sum([1,2,3,[4,5],[10]]))
print(nested_sum([]))
print(nested_sum([[1],[2],[3],[4],[5],[10]]))
print(nested_sum([1,2,3,[-4,-5],[10]]))
print(nested_sum([1,2,3,[[-4,[-5]],[10]]]))
#END