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


Groups > comp.lang.python > #63128

Re: [newbie] Recursive algorithm - review

Path csiph.com!usenet.pasdenom.info!aioe.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed4.news.xs4all.nl!xs4all!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.001
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; '"this': 0.03; 'subject:: [': 0.04; 'broken': 0.04; 'pop': 0.05; 'great.': 0.07; 'nicely': 0.07; 'string': 0.09; 'directive.': 0.09; 'exception.': 0.09; 'lines.': 0.09; 'throws': 0.09; 'violates': 0.09; 'cc:addr:python- list': 0.11; 'jan': 0.12; 'random': 0.14; "wouldn't": 0.14; 'before.': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'gonna': 0.16; 'overriding': 0.16; 'written.': 0.16; 'sat,': 0.16; ':-)': 0.16; 'ignore': 0.16; 'wrote:': 0.18; 'bit': 0.19; 'later': 0.20; 'not,': 0.20; 'subject:] ': 0.20; '>>>': 0.22; 'programming': 0.22; 'cc:addr:python.org': 0.22; "aren't": 0.24; 'earlier': 0.24; 'looks': 0.24; 'cc:2**0': 0.24; 'pass': 0.26; 'header:In-Reply-To:1': 0.27; 'function': 0.29; 'chris': 0.29; 'start,': 0.30; 'statement': 0.30; 'message- id:@mail.gmail.com': 0.30; "i'm": 0.30; 'code': 0.31; 'comments': 0.31; 'probably': 0.32; 'cases': 0.33; 'checking': 0.33; 'advice': 0.35; 'something': 0.35; 'but': 0.35; 'received:google.com': 0.35; "didn't": 0.36; 'should': 0.36; 'so,': 0.37; 'turn': 0.37; 'list': 0.37; 'list.': 0.37; 'sometimes': 0.38; 'thank': 0.38; 'branch': 0.38; 'pm,': 0.38; 'functional': 0.39; 'how': 0.40; 'even': 0.60; 'free': 0.61; 'tips': 0.61; 'new': 0.61; 'simply': 0.61; "you're": 0.61; 'save': 0.62; 'happen': 0.63; 'offering': 0.63; 'more': 0.64; 'believe': 0.68; 'fact,': 0.69; 'prime': 0.74; 'other.': 0.75; "'if": 0.84; 'confusing': 0.84; 'it"': 0.84; 'points,': 0.84; 'to:none': 0.92; 'from.': 0.93
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:cc :content-type; bh=r0Le50AlEoFglGo/R0JvnhKpvFpmBwikvILynbrD3hc=; b=EVKYbz+kv9yzH1t0skMR4CoBHOOEpZ4Ih5qay9WO+/tuRlciMGXwiWpJnyspmKGeMK UKf15JwtrWOPNd0a3FbuSrJYtYNbDyNNai8BxZVmVAoM4lcdI8TtnYOSyCcpU3NWPrG0 Vb+rhbhg5AfJ36yUwaaXxtHuiZ4SyFllapsnCxu4MMJZkNudh8OKQXc89IvBkpKZDGjU e9hoaolE0H2y2VKs+YcA9Ab4r4+Medb7xxzVmikqV7uHyco4dgrg7Ynu4JRgnzhzcQ0E ALICv01eNvEuP3YC8WSozBNgrD37KEoxGVddDkcuATMbbpeRq+WHU/S0hL1hUFdfoVVJ X5Mw==
MIME-Version 1.0
X-Received by 10.68.247.6 with SMTP id ya6mr97411430pbc.45.1388834289573; Sat, 04 Jan 2014 03:18:09 -0800 (PST)
In-Reply-To <wxmfswg8catf$.1t59xvw5p7mat.dlg@40tude.net>
References <dn7in1f4s0kq$.1vv43kwkd9tvo$.dlg@40tude.net> <mailman.4875.1388800960.18130.python-list@python.org> <wxmfswg8catf$.1t59xvw5p7mat.dlg@40tude.net>
Date Sat, 4 Jan 2014 22:18:09 +1100
Subject Re: [newbie] Recursive algorithm - review
From Chris Angelico <rosuav@gmail.com>
Cc "python-list@python.org" <python-list@python.org>
Content-Type text/plain; charset=UTF-8
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 <https://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 <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.4904.1388834298.18130.python-list@python.org> (permalink)
Lines 52
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1388834298 news.xs4all.nl 2965 [2001:888:2000:d::a6]:40008
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:63128

Show key headers only | View raw


On Sat, Jan 4, 2014 at 10:09 PM, Wiktor <look@signature.invalid> wrote:
> On Sat, 4 Jan 2014 13:02:37 +1100, Chris Angelico wrote:
>> And in fact, you might want to turn this whole branch into something
>> that harks to a more functional programming style:
>>
>> return all((check(towers, i) for i in range(len(towers)))
>
>   Great. I didn't know all() before. :-)
>   Now check() function looks very neat.

Sometimes they aren't any help at all (puns intended), but sometimes
any() and all() are exactly what you want.

>   Although 'if' statement must now looks like: 'if x is not None:'. Earlier x
> never was going to be 0. Now it can be.

Nicely spotted, I hadn't thought of that implication.

>>>         random.shuffle(row)                    # at every recursion
>>
>> Again, I wouldn't wrap comments onto unrelated lines. You see how
>> confusing this looks, now that I take this line out of context? Same
>> will happen if it throws an exception.
>
>   Yeap. Now I see it. Never gonna do that again. :-)

Please note that I didn't intend this as criticism, or a "this is the
rule so follow it" directive, just as advice :) I'm not bearing down
on you with a sergeant-major's string of orders, just offering some
tips that you're free to ignore if you like. And in fact, you probably
WILL ignore some of them, at some points, even if you believe them to
be good advice now - every stylistic rule must be secondary to the
overriding rule "Make it work and be readable", and should be broken
if it violates the prime directive.

>> This is the same as you have at the top of 'if not towers'. Can you be
>> confident that row is None any time towers is None? If so, just move
>> this up above the other check and save the duplication.
>
>   row is None at start, but later it is list - sometimes an empty list. For
> that cases this if statement was written. If row == [] -> generate new random
> row that I can pop out from.

Yes, but will you ever pass a non-None row and a None towers? If not,
you can deduplicate that bit of code by simply checking one before the
other.

>   Thank you for all Your comments.

My pleasure! Always happy to help out.

ChrisA

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


Thread

Re: [newbie] Recursive algorithm - review Chris Angelico <rosuav@gmail.com> - 2014-01-04 13:02 +1100
  Re: [newbie] Recursive algorithm - review Wiktor <look@signature.invalid> - 2014-01-04 12:09 +0100
    Re: [newbie] Recursive algorithm - review Chris Angelico <rosuav@gmail.com> - 2014-01-04 22:18 +1100
      Re: [newbie] Recursive algorithm - review Wiktor <look@signature.invalid> - 2014-01-04 12:53 +0100
      Re: [newbie] Recursive algorithm - review Wiktor <look@signature.invalid> - 2014-01-04 14:01 +0100

csiph-web