Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #50105

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

Newsgroups comp.lang.python
Date 2013-07-07 09:13 -0700
References <e3ab7b0a-d6cb-454c-aa8a-80cf4e3fc569@googlegroups.com>
Message-ID <b50b60a5-aef0-4a8b-b9bb-7901bb10e633@googlegroups.com> (permalink)
Subject Re: Simple recursive sum function | what's the cause of the weird behaviour?
From Russel Walker <russ.pobox@gmail.com>

Show all headers | View raw


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 discovered something about lists that explains the very first "weird" result I was observing, which I realized was because lists are mutable etc, but more specifically:

This

>>> a = [1, 2]
>>> a += [3]

is equivalent to, AFAIK, this

>>> a = [1, 2]
>>> a.extend([3])

So to overcome that you just have to do

>>> a = [1, 2]
>>> a = a + [3]

Which creates a new list. So any variables which were pointing to the same list as a, are unaffected.

Summary
- - - -

>>> # --- Bad ---
>>> a = [1, 2]
>>> b = a
>>> a += [3]
>>> print a
[1, 2, 3]
>>> print b
[1, 2, 3]

>>> # --- Good ---
>>> a = [1, 2]
>>> b = a
>>> a = a + [3]
>>> print a
[1, 2, 3]
>>> print b
[1, 2]


And as for the testbench:

def supersum(seq, start=0):
    return


# ---------------- Testing -------------------------------- >

testcases = [

    # (seq,                 start, result)

    # arithmetic sums
    ([],                    0,      0),
    ([[], []],              0,      0),
    ([[], [[],[]]],         0,      0),
    ([1],                   0,      1),
    ([[], [1]],             0,      1),
    ([[], [[],[1, 1]]],     0,      2),
    ([[1], [1]],            0,      2),
    ([[1], [[1],[1, 1]]],   0,      4),
    ([[1], [[1],[1, 1]]],   1,      5),

    # list flattening
    ([],                    [],     []),
    ([[], []],              [],     []),
    ([[], [[],[]]],         [],     []),
    ([],                    [1],    [1]),
    ([[], []],              [1],    [1]),
    ([[], [[],[]]],         [1],    [1]),
    ([1],                   [1],    [1, 1]),
    ([[1], [1]],            [1],    [1, 1]),
    ([[1], [[1],[1]]],      [1],    [1, 1, 1, 1]),

    ]


for seq, start, result in testcases:
    try:
        assert supersum(seq, start) == result
    except Exception as er:
        print "seq:%s\t start:%s" % (seq, start)
        if type(er) is AssertionError:
            print "expected:", result
            print "got:     ", supersum(seq, start)
        else:
            print repr(er)
        print ''

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Simple recursive sum function | what's the cause of the weird behaviour? Russel Walker <russ.pobox@gmail.com> - 2013-07-06 05:37 -0700
  Re: Simple recursive sum function | what's the cause of the weird behaviour? Russel Walker <russ.pobox@gmail.com> - 2013-07-06 05:54 -0700
  Re: Simple recursive sum function | what's the cause of the weird behaviour? Russel Walker <russ.pobox@gmail.com> - 2013-07-06 05:59 -0700
    Re: Simple recursive sum function | what's the cause of the weird behaviour? Peter Otten <__peter__@web.de> - 2013-07-06 15:19 +0200
    Re: Simple recursive sum function | what's the cause of the weird behaviour? Joshua Landau <joshua.landau.ws@gmail.com> - 2013-07-06 19:43 +0100
      Re: Simple recursive sum function | what's the cause of the weird behaviour? Rotwang <sg552@hotmail.co.uk> - 2013-07-06 21:10 +0100
        Re: Simple recursive sum function | what's the cause of the weird behaviour? Rotwang <sg552@hotmail.co.uk> - 2013-07-06 21:25 +0100
  Re: Simple recursive sum function | what's the cause of the weird behaviour? Chris Angelico <rosuav@gmail.com> - 2013-07-07 03:22 +1000
  Re: Simple recursive sum function | what's the cause of the weird behaviour? Terry Reedy <tjreedy@udel.edu> - 2013-07-06 14:47 -0400
  Re: Simple recursive sum function | what's the cause of the weird behaviour? Russel Walker <russ.pobox@gmail.com> - 2013-07-07 09:13 -0700
  Re: Simple recursive sum function | what's the cause of the weird behaviour? Russel Walker <russ.pobox@gmail.com> - 2013-07-07 09:44 -0700

csiph-web