I need to write a function nested_sum(L) that will sum all the ints inside a list no matter if they are inside another list. This with calling recrusively to another function mult2(n).
Example:
>>> nestedSum(mult2( [1,['a',3,'b',2],[4,['h',8,[10]]], -5]))
24
I tried to code this:
def mult2(n):
if type(n) == int and n%2 ==0:
return n
def nested_sum(L):
total = 0
for i in L:
if isinstance(i, list):
total += nested_sum(i)
else:
total += i
return total
And unfortanetly I can't change the code of mult2(n) function. I can only change the nested_sum(L) function.
Can someone please give me a clue what to do? Thank you.