forked from hcientist/OnlinePythonTutor
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwentworth_sumList.txt
More file actions
25 lines (21 loc) · 670 Bytes
/
wentworth_sumList.txt
File metadata and controls
25 lines (21 loc) · 670 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
# Tutorial code from Prof. Peter Wentworth
# Rhodes University, South Africa (http://www.ru.ac.za/)
def sumList(xs):
'''
Sum a list that can contain nested lists.
Precondition: All leaf elements are numbers.
'''
sum = 0
for e in xs:
if type(e) is list:
print("Calling sumList(%s) recursively" % e)
v = sumList(e)
print("sumList(%s) returned %s" % (e, v))
sum += v
else:
sum += e
return sum
testData = [10, [20, 30, [40], 50], 60]
print("Calling sumList(%s)" % testData)
result = sumList(testData)
print("Final sum of all numbers in initial list is %s" % result)