-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsum_recursive.py
More file actions
31 lines (27 loc) · 842 Bytes
/
Copy pathsum_recursive.py
File metadata and controls
31 lines (27 loc) · 842 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
25
26
27
28
29
30
31
#!/usr/bin/env python3
"""
A Function that sums up all the values in a nested number list
"""
def r_sum(nested_num_list):
""" (list) -> float
Sum up the values in a nested numbers list
"""
cntr = 0
for elem in nested_num_list: # Traverse the list
# Recursive call for nested lists
if isinstance(elem, list):
cntr += r_sum(elem)
# Base case
elif isinstance(elem, (int, float)):
cntr += elem
else:
raise TypeError('Invalid value found in list: {0}'.format(elem))
return cntr
def main():
print(r_sum([4]))
print(r_sum([3, 4]))
print(r_sum([3, 4, 5.6]))
print(r_sum([3, 3, 3, 3, 4, 1, 9, 44, -2, 8, 8]))
print(r_sum([3, 3, 3, 3, 'four', 1, 9, 44, -2, 8, 8]))
if __name__ == '__main__':
main()