Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.net!feeder.erje.net!eu.feeder.erje.net!newsfeed.freenet.ag!news2.euro.net!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.009 X-Spam-Evidence: '*H*': 0.98; '*S*': 0.00; 'string': 0.09; 'inserted': 0.09; 'works.': 0.09; 'python': 0.11; 'def': 0.12; 'posted': 0.15; 'any.': 0.16; 'backwards': 0.16; 'excluded': 0.16; 'quoted': 0.16; 'received:74.208.4.195': 0.16; 'recurse': 0.16; 'seconds.': 0.16; 'set()': 0.16; 'skip:r 80': 0.16; 'solver': 0.16; 'wrote:': 0.18; 'seems': 0.21; 'print': 0.22; 'header:User-Agent:1': 0.23; '2.x': 0.24; 'fine': 0.24; 'downloaded': 0.26; 'somewhere': 0.26; 'header :In-Reply-To:1': 0.27; 'am,': 0.29; 'character': 0.29; 'code': 0.31; 'block,': 0.31; 'anyone': 0.31; 'another': 0.32; 'worked': 0.33; 'older': 0.33; 'at:': 0.34; 'but': 0.35; 'there': 0.35; 'representing': 0.36; "didn't": 0.36; '2008': 0.38; 'to:addr :python-list': 0.38; 'explain': 0.39; 'bill': 0.39; 'to:addr:python.org': 0.39; 'how': 0.40; 'read': 0.60; 'simple': 0.61; 'show': 0.63; 'places': 0.64; 'worth': 0.66; 'received:74.208': 0.68; "it'd": 0.84; 'apparent': 0.91 Date: Wed, 27 Mar 2013 05:38:48 -0400 From: Dave Angel User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:17.0) Gecko/20130308 Thunderbird/17.0.4 MIME-Version: 1.0 To: python-list@python.org Subject: Re: Sudoku References: In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit X-Provags-ID: V02:K0:NWeg3HVt09frYnBmkJoY3PbXYvI4KGH8VfoU/yobEbX Gtza4XVDXovcRfOYbsAM2p9ygx8iGSAlxN5cs60e8fVJrTKrNh oIXAdR5IGoMc7zzBS0CRP5w8o7WscPVppPOGopH5ICw0d3KCYe kQlWWc758ZkV7+5GlE8jGoWcfXzE8eGmjA8Z5WZO629R3fJcEP Dv447hmHe1mb4CeUNpM1t8mSZVqjtjQ7SimC9gRjS+iQ1ywWF3 G5720g48Ha/kI7scvGPHsMOTV4h8SqD92oIjjXPCO4673edGJI SSceRRS5eTF8FXLzzygToYTpD2OMrU/cmEbiQa2F6ahfdB3dQ= = 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: 50 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1364377147 news.xs4all.nl 6948 [2001:888:2000:d::a6]:52639 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:41999 On 03/27/2013 01:44 AM, Eric Parry wrote: > I downloaded the following program from somewhere It'd be good to show where you found it, and credit the apparent author. Bill Barksdale posted this in 2008 at: http://stackoverflow.com/questions/201461/shortest-sudoku-solver-in-python-how-does-it-work I don't know if there are older ones somewhere, but I didn't find any. I did find places that quoted his code without attribution. Another thing worth pointing out is that it's only valid for Python 2.x (naturally, since I don't think Python 3 was out at that point) using a link from Wikipedia and inserted the “most difficult Sudoku puzzle ever” string into it and ran it. It worked fine and solved the puzzle in about 4 seconds. However I cannot understand how it works. It seems to go backwards and forwards at random. Can anyone explain how it works in simple terms? > Eric. > > > def same_row(i,j): return (i/9 == j/9) > def same_col(i,j): return (i-j) % 9 == 0 > def same_block(i,j): return (i/27 == j/27 and i%9/3 == j%9/3) > > def r(a): > i = a.find('0') > if i == -1: > print a > exit(a) > > excluded_numbers = set() > for j in range(81): > if same_row(i,j) or same_col(i,j) or same_block(i,j): > excluded_numbers.add(a[j]) > > for m in '123456789': > if m not in excluded_numbers: > # At this point, m is not excluded by any row, column, or block, so let's place it and recurse > r(a[:i]+m+a[i+1:]) > > r('800000000003600000070090200050007000000045700000100030001000068008500010090000400') > Sudoku solver where the puzzle is an 81 character string representing the puzzle read left-to-right, top-to-bottom, and 0 is a blank. >   > -- DaveA