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


Groups > comp.lang.python > #85173

Re: Idiomatic backtracking in Python

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 <davea@davea.name>
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 <davea@davea.name>
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 <ma3itj$ol6$1@news.albasani.net> <mailman.18136.1422232323.18130.python-list@python.org> <87bnlml44b.fsf@elektro.pacujo.net> <CAPTjJmr5Bp7oi9mNg9TqGwkXng38SSik+q7xcWfHuRXjgP_fgg@mail.gmail.com>
In-Reply-To <CAPTjJmr5Bp7oi9mNg9TqGwkXng38SSik+q7xcWfHuRXjgP_fgg@mail.gmail.com>
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 <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.18441.1422998174.18130.python-list@python.org> (permalink)
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

Show key headers only | View raw


On 01/25/2015 08:45 PM, Chris Angelico wrote:
> On Mon, Jan 26, 2015 at 12:31 PM, Marko Rauhamaa <marko@pacujo.net> 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

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


Thread

Idiomatic backtracking in Python Johannes Bauer <dfnsonfsduifb@gmx.de> - 2015-01-25 21:15 +0100
  Re: Idiomatic backtracking in Python Ian Foote <ian@feete.org> - 2015-01-25 20:51 +0000
    Re: Idiomatic backtracking in Python Rustom Mody <rustompmody@gmail.com> - 2015-01-25 18:41 -0800
      Re: Idiomatic backtracking in Python Jussi Piitulainen <jpiitula@ling.helsinki.fi> - 2015-01-26 09:21 +0200
        Re: Idiomatic backtracking in Python Rustom Mody <rustompmody@gmail.com> - 2015-01-25 23:28 -0800
  Re: Idiomatic backtracking in Python Ben Finney <ben+python@benfinney.id.au> - 2015-01-26 11:32 +1100
    Re: Idiomatic backtracking in Python Marko Rauhamaa <marko@pacujo.net> - 2015-01-26 03:31 +0200
      Re: Idiomatic backtracking in Python Chris Angelico <rosuav@gmail.com> - 2015-01-26 12:45 +1100
      Re: Idiomatic backtracking in Python Dave Angel <davea@davea.name> - 2015-02-03 16:16 -0500
      Re: Idiomatic backtracking in Python Chris Angelico <rosuav@gmail.com> - 2015-02-04 09:29 +1100
  Re: Idiomatic backtracking in Python MRAB <python@mrabarnett.plus.com> - 2015-01-26 00:43 +0000
  Re: Idiomatic backtracking in Python Mark Lawrence <breamoreboy@yahoo.co.uk> - 2015-01-26 08:06 +0000
  Re: Idiomatic backtracking in Python sjmsoft@gmail.com - 2015-01-27 04:48 -0800

csiph-web