-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsumNested.py
More file actions
25 lines (22 loc) · 729 Bytes
/
sumNested.py
File metadata and controls
25 lines (22 loc) · 729 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
def sumList(givenList,totalSum = 0,i = 0):
if len(givenList) == i:
return totalSum
else:
totalSum += givenList[i]
i += 1
return sumList(givenList,totalSum,i)
def sumNested(givenList, totalSum = 0,i = 0):
if len(givenList) == i:
return totalSum
elif type(givenList[i]) == int:
totalSum += givenList[i]
i += 1
return sumNested(givenList,totalSum,i)
elif type(givenList[i]) == list:
tempSum = sumList(givenList[i])
totalSum += tempSum
return sumNested(givenList,totalSum,i+1)
givenList = [1,[5],6,[1,2,4],5]
totalSum = sumNested(givenList)
print('Given List',givenList)
print('Sum of Elements of Nested List is',totalSum)