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


Groups > comp.lang.python > #84580

Re: Idiomatic backtracking in Python

Path csiph.com!usenet.pasdenom.info!news.etla.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed2.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <ian@feete.org>
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; 'say,': 0.05; 'subject:Python': 0.06; '(python': 0.07; 'detect': 0.07; 'none:': 0.07; 'skip:` 10': 0.07; 'grid': 0.09; 'pgp': 0.09; 'skip:t 60': 0.09; 'def': 0.12; 'accepting': 0.14; "wouldn't": 0.14; '----- begin': 0.16; '-----end': 0.16; '3):': 0.16; 'exit.': 0.16; 'folks,': 0.16; 'from:addr:ian': 0.16; 'gnupg': 0.16; 'hash:': 0.16; 'parameter.': 0.16; 'pythonic': 0.16; 'sha1': 0.16; 'solver': 0.16; 'appropriate': 0.16; 'wrote:': 0.18; 'message-----': 0.19; 'solution.': 0.20; 'fit': 0.20; 'example': 0.22; 'coding': 0.22; 'header:User-Agent:1': 0.23; 'rid': 0.24; 'cheers,': 0.24; 'defined': 0.27; 'signed': 0.27; 'header:In- Reply-To:1': 0.27; 'have,': 0.30; "i'm": 0.30; 'code': 0.31; 'cells': 0.31; 'received:66.33': 0.31; 'received:66.33.216': 0.31; 'received:66.33.216.122': 0.31; 'received:dreamhost.com': 0.31; 'received:g.dreamhost.com': 0.31; 'received:hapkido.dreamhost.com': 0.31; 'skip:i 60': 0.31; 'skip:b 30': 0.33; "i'd": 0.34; 'could': 0.34; 'problem': 0.35; 'received:66': 0.35; "can't": 0.35; 'problem.': 0.35; 'but': 0.35; 'skip:s 60': 0.36; 'version:': 0.36; 'yield': 0.36; 'hi,': 0.36; 'should': 0.36; 'example,': 0.37; 'so,': 0.37; 'two': 0.37; 'implement': 0.38; 'problems': 0.38; 'filled': 0.38; 'solving': 0.38; 'to:addr:python-list': 0.38; 'to:addr:python.org': 0.39; 'ian': 0.60; 'new': 0.61; 'strictly': 0.61; 'hear': 0.63; 'more': 0.64; 'love': 0.65; 'feeling': 0.68; 'invalid': 0.68; 'hand': 0.80; 'partially': 0.84; 'problems?': 0.91; 'received:192.168.0.4': 0.91
DKIM-Signature v=1; a=rsa-sha1; c=relaxed; d=feete.org; h=message-id :date:from:mime-version:to:subject:references:in-reply-to :content-type:content-transfer-encoding; s=feete.org; bh=rqqcNRx u5IXTv7cmDQqcxvdKSgk=; b=DEPPTmMVPMK+JVDyyiOdzNIJJT4N0cZieIjELl5 W7g3w3YSRV08wolxjqTFs2V2e30v6GVu4XkHR0GTkbZBJhw+s4cz5eL3ivbxAywq zHqOAOL1Dt6bnCJ/RL4KTAG3eu5e/RZhAqyoDE8aYcla2QFspt6e/v3Ne4Wc679J KfyE=
Date Sun, 25 Jan 2015 20:51:03 +0000
From Ian Foote <ian@feete.org>
User-Agent Mozilla/5.0 (X11; Linux x86_64; rv:31.0) Gecko/20100101 Thunderbird/31.3.0
MIME-Version 1.0
To python-list@python.org
Subject Re: Idiomatic backtracking in Python
References <ma3itj$ol6$1@news.albasani.net>
In-Reply-To <ma3itj$ol6$1@news.albasani.net>
Content-Type text/plain; charset=utf-8
Content-Transfer-Encoding quoted-printable
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.18134.1422219082.18130.python-list@python.org> (permalink)
Lines 69
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1422219082 news.xs4all.nl 2860 [2001:888:2000:d::a6]:42315
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:84580

Show key headers only | View raw


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Hi,

I think a very idiomatic way to implement backtracking is using a
recursive generator (python 3):

def backtrack_solver(data=None):
    if data is None:
        yield from backtrack_solver(data=initial_data)

    if cannot_be_valid(data):
        return

    if matches_condition(data):
        yield data
        return

    for new_data in process(data):
        yield from backtrack_solver(new_data)

This generator will yield valid solutions to a suitably defined problem.

`initial_data`, `cannot_be_valid`, `matches_condition` and `process`
should be replaced with appropriate implementation for your problem.

For example, a sudoku solver could be fit to this by accepting a
partially solved grid as the `data` parameter.

`cannot_be_valid` would now detect grids that have, say, two `1`s in a
row or any other invalid grid state and exit.

`matches_condition` would detect a fully solved grid.

`process` would produce new grids with more cells filled in than the
current grid.

`initial_data` wouldn't be strictly necessary here, but you could use
it for an example grid. It could also be an empty grid, and the solver
would then yield all valid grids.

Regards,
Ian F

On 25/01/15 20:15, Johannes Bauer wrote:
> Hi folks,
> 
> I have a problem at hand that needs code for backtracking as a
> solution. And I have no problem coding it, but I can't get rid of
> the feeling that I'm always solving backtracking problems in a
> non-Pythonic (non-idiomatic) way. So, I would like to ask if you
> have a Pythonic approach to backtracking problems? If so, I'd love
> to hear your solutions!
> 
> Cheers, Johannes
> 

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1

iQEcBAEBAgAGBQJUxVc3AAoJEODsV4MF7PWzO+sH/jaz0Dc7Hs9LkbB8g6//A7pK
bxBeFSVtmvaHynASg2PRAzSAC4dty5R52myPoXB3Hdf+otTjBUjOyA7k5j+HCDum
TeJJSUFwOFQxr3yRtXcYoct+xYGBAGRqjT0oiGJMFYp5dLPXmHsAv10KIr3HcOo4
TgqQ9XtyMw60Tmx1ZJ/pj0xOPtrr5PUxe0bwRC5bRycDS943s+UJ/o42DhnBtkZp
h6kkqsZsAL27i0hZrqBEfWMaIHbY9DZNzA9PYyYEl/pzvtB0tpN6ENrxTQFbBNeE
SZoEz9AdcUr9D0ej3HaTgmbT/ivl0op4xQdnpp75uRnGpaH5LlssEGbWQsmRwsY=
=Jpwv
-----END PGP SIGNATURE-----

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