i got this task today from school to make a function Sum(list): i researched and found out few ways how to make it work with list that consist of int, or list of list of integers, however my function should work also with other types like string, or tuples. Here are some examples of what it should do.
Sum([1,2,3]) returns 6; Sum([2,[1.5,3],0.5]) returns 7.0;
Sum([[['p'],'yt'],['h',['on']]]) returns 'python'
Sum([[(1,2)],(3,),[(4,5)]]) returns (1, 2, 3, 4, 5);
And on top of that all it should be also able to compute if argument in the function Sum(X) is NOT list. Example:
Sum(5*5) returns 25;
This is what i am working with so far, works fine for first 2 cases, however i cant find a way to modify it to work for other examples. Personaly ive been trying to use isinstance and exceptions
def list_sum(L):
total = 0
for i in L:
if isinstance(i, list):
total += list_sum(i)
else:
total += i
return total
Many thanks for any kind of help