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
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:
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)
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
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
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:
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
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 +=
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:
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:
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
11 matches
Mail list logo