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


Groups > comp.lang.python > #50071

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

Path csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!xlned.com!feeder5.xlned.com!news2.euro.net!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <rosuav@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.026
X-Spam-Evidence '*H*': 0.95; '*S*': 0.00; 'element': 0.07; 'list?': 0.07; '[1,': 0.09; 'append': 0.09; '[])': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'recurse': 0.16; 'sorts': 0.16; 'sat,': 0.16; 'wrote:': 0.18; 'header:In-Reply-To:1': 0.27; 'message-id:@mail.gmail.com': 0.30; 'code': 0.31; '>>>>': 0.31; 'subject:what': 0.31; 'not.': 0.33; 'subject:the': 0.34; 'problem': 0.35; "can't": 0.35; 'received:google.com': 0.35; 'subject:Simple': 0.36; 'subject:?': 0.36; 'should': 0.36; 'list': 0.37; 'to:addr:python-list': 0.38; 'list,': 0.38; 'pm,': 0.38; 'does': 0.39; 'to:addr:python.org': 0.39; 'either': 0.39; 'kind': 0.63; 'jul': 0.74; 'not:': 0.91; '2013': 0.98
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :content-type; bh=k0Hp0OApyhQ6ekf8yzmhXzZChFQ781oscZ9r+eyyjMU=; b=j8wUNQNXAwqSGrGNFqny0bteJtVkUDohePVGD4anW3MwY5Xceyfqyy5jHf1i9naxMl UNOw2k88eKvu1uyToV89TJOddOyhcVr8D+lU3PL/sWUxi8+OsEB6+bFZeFSp/7lUQaLS ao6Xxd5CP78uu5PfLXa2yVafHbx7YtLpbLn6jEw/S8vIMU3RSEg8Yi7x6YHgl8R9avSv v8rIEWS3UbQorj2Yjc/tihjmVZCxXr1CMFfxx8soLYj/2iH8pqUgPeyqKlV0L3s1tpjf QXWTw4pADMlNLYoONQygZBo3qVWCSssrxz8j48msYfwag7/PkFp6fTpyTifu5cQ0B3qT 3glA==
MIME-Version 1.0
X-Received by 10.220.182.193 with SMTP id cd1mr10508808vcb.32.1373131360714; Sat, 06 Jul 2013 10:22:40 -0700 (PDT)
In-Reply-To <e3ab7b0a-d6cb-454c-aa8a-80cf4e3fc569@googlegroups.com>
References <e3ab7b0a-d6cb-454c-aa8a-80cf4e3fc569@googlegroups.com>
Date Sun, 7 Jul 2013 03:22:40 +1000
Subject Re: Simple recursive sum function | what's the cause of the weird behaviour?
From Chris Angelico <rosuav@gmail.com>
To python-list@python.org
Content-Type text/plain; charset=ISO-8859-1
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.4340.1373131363.3114.python-list@python.org> (permalink)
Lines 28
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1373131363 news.xs4all.nl 15943 [2001:888:2000:d::a6]:57982
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:50071

Show key headers only | View raw


On Sat, Jul 6, 2013 at 10:37 PM, Russel Walker <russ.pobox@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 here. What should supersum do with
the list [1]? Should it recurse into it, or append it as a list? It
can't do both. For a list flattener, you would need to either use
.append for each element you come across, or .extend with each list,
with some kind of check to find whether you should recurse or not.

Still, it's a fun thing to play with. I like code golfing these sorts
of trinketty functions, just for fun :)

ChrisA

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