On 06/07/2013 19:43, Joshua Landau wrote:
On 6 July 2013 13:59, Russel Walker <russ.po...@gmail.com> wrote:
Since I've already wasted a thread I might as well...
Does this serve as an acceptable solution?
def supersum(sequence, start=0):
result = type(start)()
for item in sequence:
try:
result += supersum(item, start)
except:
result += item
return result
It's probably more robust to do:
def supersum(sequence, start=0):
for item in sequence:
try:
result = result + supersum(item, start)
except:
result = result + item
return result
I assume you meant to put "result = start" in there at the beginning.
as that way you aren't assuming the signature of type(start).
It's not quite clear to me what the OP's intentions are in the general
case, but calling supersum(item, start) seems odd - for example, is the
following desirable?
>>> supersum([[1], [2], [3]], 4)
22
I would have thought that the "correct" answer would be 10. How about
the following?
def supersum(sequence, start = 0):
result = start
for item in reversed(sequence):
try:
result = supersum(item, result)
except:
result = item + result
return result
--
http://mail.python.org/mailman/listinfo/python-list