Simple recursive sum function | what's the cause of the weird behaviour?
Russel Walker
russ.pobox at gmail.com
Sun Jul 7 12:44:42 EDT 2013
I got it! One of the testcases was wrong,
([[1], [1]], [1], [1, 1]),
should be
([[1], [1]], [1], [1, 1, 1]),
And the working solution.
def supersum(sequence, start=0):
result = start
start = type(start)()
for item in sequence:
try:
item = supersum(item, start)
except TypeError:
pass
try:
result = result + item
except TypeError:
return result + sequence
return result
I couldn't yet get around doing type(start)() and it's pretty messy, but anyways...
More information about the Python-list
mailing list