Path: csiph.com!usenet.pasdenom.info!goblin2!goblin.stu.neva.ru!newsfeed.xs4all.nl!newsfeed1a.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.036 X-Spam-Evidence: '*H*': 0.93; '*S*': 0.00; 'that?': 0.05; 'tree': 0.05; 'subject:Python': 0.06; 'discard': 0.07; 'plenty': 0.07; 'caller': 0.09; 'function,': 0.09; '\xe2\x80\x94': 0.09; 'def': 0.12; 'jan': 0.12; 'levels,': 0.16; 'node.': 0.16; 'return,': 0.16; 'somehow,': 0.16; 'variable.': 0.16; 'wrote:': 0.18; 'obviously': 0.18; 'trying': 0.19; 'example': 0.22; 'header:User- Agent:1': 0.23; "aren't": 0.24; 'skip': 0.24; 'mon,': 0.24; "i've": 0.25; 'define': 0.26; 'possibly': 0.26; 'least': 0.26; 'header:In-Reply-To:1': 0.27; 'chris': 0.29; 'once,': 0.31; 'could': 0.34; 'problem': 0.35; "can't": 0.35; 'but': 0.35; 'there': 0.35; 'done': 0.36; 'two': 0.37; 'level': 0.37; 'implement': 0.38; 'branch': 0.38; 'to:addr:python-list': 0.38; 'pm,': 0.38; 'that,': 0.38; 'to:addr:python.org': 0.39; 'how': 0.40; 'skip:u 10': 0.60; 'solve': 0.60; 'break': 0.61; 'entire': 0.61; 'simply': 0.61; "you're": 0.61; 'times': 0.62; 'places': 0.64; 'become': 0.64; 'more': 0.64; 'levels': 0.65; 'situation': 0.65; 'within': 0.65; '26,': 0.68; 'received:74.208': 0.68; 'discovered': 0.83; '2015': 0.84; 'naughty': 0.84; 'received:74.208.4.194': 0.84; 'returns.': 0.84; 'examine': 0.93; 'imagine': 0.93 Date: Tue, 03 Feb 2015 16:16:02 -0500 From: Dave Angel User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:31.0) Gecko/20100101 Thunderbird/31.4.0 MIME-Version: 1.0 To: python-list@python.org Subject: Re: Idiomatic backtracking in Python References: <87bnlml44b.fsf@elektro.pacujo.net> In-Reply-To: Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 8bit X-Provags-ID: V02:K0:8B6S8R/OhrHKV84EzVnhR01ptfUY9jkT2B/CU7gyyoE pvg9ZTTiRhbTP8Hrg4lDTGQ2pr23qAdzlfrcYOph/zwrmDUNMt st8F3Ycrgu3TYNb1L3NbhNze7PiYlgeWRMZ9JKKKh40nluKqfz dlLy7YB84wkc7qogjydBGwYtxV1WPPnM1yP3ehMX5r/6yhxkl4 x8fhTG8hakQO4W0D6sv+21Nq+IMN1db2dqTcDmDNlEcu8sWkFn WZ2TIZ/0C0qHerOR7FX6M9POzytRxxipgxtYb/ucin+QzIle8K yOgwb4TxO6r74JywfZ0OBZhjcxu3O5Oc5mn5X9OLnZ2klxM37r In/mf2NqJfnxC1j0gvOI= X-UI-Out-Filterresults: notjunk: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 List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 38 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1422998174 news.xs4all.nl 2861 [2001:888:2000:d::a6]:53387 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:85173 On 01/25/2015 08:45 PM, Chris Angelico wrote: > On Mon, Jan 26, 2015 at 12:31 PM, Marko Rauhamaa wrote: >> Backtracking means the part of depth-first traversal where you retreat >> to the parent node. If you implement your traversal with a recursive >> function, backtracking means — more or less — a return from the >> function. > > But possibly you need to return from N levels, not just one. Imagine > you're traversing a directory tree using a simplistic recursive > algorithm: > > def traverse(dir): > for d in subdirs(dir): traverse(d) > for f in files(dir): > if file_size(f) > 1024*1024*1024: skip this entire branch of the tree > > 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. -- DaveA