Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #50105
| X-Received | by 10.224.205.138 with SMTP id fq10mr21034409qab.1.1373213599285; Sun, 07 Jul 2013 09:13:19 -0700 (PDT) |
|---|---|
| X-Received | by 10.49.35.195 with SMTP id k3mr404423qej.2.1373213599266; Sun, 07 Jul 2013 09:13:19 -0700 (PDT) |
| Path | csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!usenet.blueworldhosting.com!feeder02.blueworldhosting.com!news.ripco.com!news.glorb.com!t19no655809qam.0!news-out.google.com!f7ni1003qai.0!nntp.google.com!t19no686648qam.0!postnews.google.com!glegroupsg2000goo.googlegroups.com!not-for-mail |
| Newsgroups | comp.lang.python |
| Date | Sun, 7 Jul 2013 09:13:19 -0700 (PDT) |
| In-Reply-To | <e3ab7b0a-d6cb-454c-aa8a-80cf4e3fc569@googlegroups.com> |
| Complaints-To | groups-abuse@google.com |
| Injection-Info | glegroupsg2000goo.googlegroups.com; posting-host=41.132.222.221; posting-account=U0dbVAoAAAAjYT-0vWHIihwl62ZNLk9Z |
| NNTP-Posting-Host | 41.132.222.221 |
| References | <e3ab7b0a-d6cb-454c-aa8a-80cf4e3fc569@googlegroups.com> |
| User-Agent | G2/1.0 |
| MIME-Version | 1.0 |
| 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> |
| Injection-Date | Sun, 07 Jul 2013 16:13:19 +0000 |
| Content-Type | text/plain; charset=ISO-8859-1 |
| Xref | csiph.com comp.lang.python:50105 |
Show key headers only | 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 | Next — Previous in thread | Next in thread | Find similar | Unroll 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