Path: csiph.com!usenet.pasdenom.info!gegeweb.org!newsfeed0.kamp.net!newsfeed.kamp.net!feeder1.cambriumusenet.nl!feed.tweaknews.nl!194.109.133.86.MISMATCH!newsfeed.xs4all.nl!newsfeed3.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.049 X-Spam-Evidence: '*H*': 0.90; '*S*': 0.00; 'that?': 0.05; 'subject:Python': 0.06; 'discard': 0.07; 'plenty': 0.07; 'remaining': 0.07; 'caller': 0.09; 'cc:addr:python-list': 0.11; 'dig': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'recurse': 0.16; 'return,': 0.16; 'somehow,': 0.16; 'variable.': 0.16; 'wrote:': 0.18; 'obviously': 0.18; 'wed,': 0.18; 'trying': 0.19; 'feb': 0.22; 'example': 0.22; 'cc:addr:python.org': 0.22; "aren't": 0.24; 'cc:2**0': 0.24; "i've": 0.25; 'define': 0.26; 'possibly': 0.26; 'least': 0.26; 'header:In-Reply-To:1': 0.27; 'am,': 0.29; 'message- id:@mail.gmail.com': 0.30; 'along': 0.30; 'code': 0.31; 'lines': 0.31; 'once,': 0.31; 'piece': 0.31; 'there,': 0.34; 'could': 0.34; 'problem': 0.35; 'basic': 0.35; "can't": 0.35; 'something': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'there': 0.35; 'done': 0.36; 'two': 0.37; 'level': 0.37; 'branch': 0.38; 'that,': 0.38; "couldn't": 0.39; 'structure': 0.39; 'space': 0.40; 'how': 0.40; 'skip:u 10': 0.60; 'dave': 0.60; 'problems.': 0.60; 'solve': 0.60; 'most': 0.60; 'break': 0.61; 'simply': 0.61; 'back': 0.62; 'times': 0.62; 'places': 0.64; 'become': 0.64; 'levels': 0.65; 'situation': 0.65; 'within': 0.65; 'believe': 0.68; 'unusual': 0.74; 'discovered': 0.83; '2015': 0.84; 'naughty': 0.84; 'returns.': 0.84; 'angel': 0.91; 'to:none': 0.92; 'examine': 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=w/LFC44A52MEpf5q/ZB1EE0eoD+DB+gftW+OdMIA98I=; b=qg4G/EKW7ygMhz2I3Ars36j639+j3vypqaY2x+pbss0P1slICyyfSKZzWoHncHUTG8 Am59eBTYZ+n+Og5oEN7cvDoQqy1WOI6bP9O89nUwjYCgzntrn2GIFOXIjDUkRXHHCcbU ZLEaiGNmIVmZfvEyyutAsXk5/RXc3nqJK5T6YzTFnEir9GWULDKfnuPnWlnJH2dD7fEh Vnf4AaUUaSucSsynTU0kqBeCkOifdfA06vtFzTLr/VB55fSqUJP32qP0Mml52I//9SWE /JYwY/AIZp/6QJ9ShG4Irh9L8xNoOcDMbxufOF7kIGYTC0Qxy4OvZ3iCX77Z3SNqaAwR 5M+g== MIME-Version: 1.0 X-Received: by 10.107.14.131 with SMTP id 125mr31307913ioo.53.1423002577724; Tue, 03 Feb 2015 14:29:37 -0800 (PST) In-Reply-To: <54D13A92.5060701@davea.name> References: <87bnlml44b.fsf@elektro.pacujo.net> <54D13A92.5060701@davea.name> Date: Wed, 4 Feb 2015 09:29:37 +1100 Subject: Re: Idiomatic backtracking in Python From: Chris Angelico Cc: "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 List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 29 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1423002586 news.xs4all.nl 2908 [2001:888:2000:d::a6]:60255 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:85181 On Wed, Feb 4, 2015 at 8:16 AM, Dave Angel wrote: >> Obviously you have to define the branch somehow, but there are plenty >> of times when you might want to break out of "everything up to here". >> How do you define that? How do you return lots of levels all at once? >> I remember facing this exact problem in trying to solve a particular >> piece-layout puzzle; if I discovered an impossible situation, I could >> actually discard at least two or three levels of recursion all at >> once, because there's no way that the situation could become >> un-impossible within those levels. Can't remember how I ended up >> dealing with that... I think I got naughty and used a global variable. >> > > When I've done things like that, there was no need to do a "return > multiple". You just return, and your caller happens to be a the end of his > loop, so he returns also. > > Classic example of this is the 8 queens puzzle. Each level is going to > examine one row, and once there are no places that aren't yet attacked, it > simply returns. That's true of most problems. I may have to go dig up the code I had, but I believe the basic structure was something along the lines of "place piece in available space, recurse to place piece in each remaining available space". Sometimes, attempting to fill one space would prove that the piece that _created_ that space couldn't possibly go there, so it would do a jump back up a few levels of recursion. Definitely an unusual situation, but not impossible. ChrisA