Re: Simple recursive sum function | what's the cause of the weird behaviour?

2013-07-07 Thread Russel Walker
I read through all of the posts and thanks for helping. What was supposed to be simple a (recursively) straightforward, turned out to be quite tricky. I've set up a small testing bench and tried all of the proposed solutions including my own but none pass. I'll post it below. I've also

Re: Simple recursive sum function | what's the cause of the weird behaviour?

2013-07-07 Thread Russel Walker
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:

Simple recursive sum function | what's the cause of the weird behaviour?

2013-07-06 Thread Russel Walker
I know this is simple but I've been starring at it for half an hour and trying all sorts of things in the interpreter but I just can't see where it's wrong. def supersum(sequence, start=0): result = start for item in sequence: try: result += supersum(item, start)

Re: Simple recursive sum function | what's the cause of the weird behaviour?

2013-07-06 Thread Russel Walker
Nevermind! Stupid of me to forget that lists or mutable so result and start both point to the same list. -- http://mail.python.org/mailman/listinfo/python-list

Re: Simple recursive sum function | what's the cause of the weird behaviour?

2013-07-06 Thread Russel Walker
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

Re: Simple recursive sum function | what's the cause of the weird behaviour?

2013-07-06 Thread Peter Otten
Russel Walker 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:

Re: Simple recursive sum function | what's the cause of the weird behaviour?

2013-07-06 Thread Chris Angelico
On Sat, Jul 6, 2013 at 10:37 PM, Russel Walker russ.po...@gmail.com wrote: This works: - - - - - - x = [[1], [2], [3]] supersum(x) 6 supersum(x, []) [1, 2, 3] This does not: - - - - - - - x = [[[1], [2]], [3]] supersum(x, []) [1, 2, 1, 2, 3] You have a problem of specification

Re: Simple recursive sum function | what's the cause of the weird behaviour?

2013-07-06 Thread Joshua Landau
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 +=

Re: Simple recursive sum function | what's the cause of the weird behaviour?

2013-07-06 Thread Terry Reedy
On 7/6/2013 8:37 AM, Russel Walker wrote: I know this is simple but I've been starring at it for half an hour and trying all sorts of things in the interpreter but I just can't see where it's wrong. def supersum(sequence, start=0): result = start for item in sequence: try:

Re: Simple recursive sum function | what's the cause of the weird behaviour?

2013-07-06 Thread Rotwang
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:

Re: Simple recursive sum function | what's the cause of the weird behaviour?

2013-07-06 Thread Rotwang
On 06/07/2013 21:10, Rotwang wrote: [...] 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