Note that there are some explanatory texts on larger screens.

plurals
  1. POPython: adding 1 print Kills my code?
    text
    copied!<p>I was playing with this sudoku solver, that I found.</p> <p>Like quoted here it works perfect, but if I uncomment that single <code>print a</code>, that I commented out (line 13), then it stops before finding a full solution...?</p> <pre><code>import sys from datetime import datetime # for datetime.now() 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('.') if i == -1: # All solved ! print a else: #print 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:]) if __name__ == '__main__': if len(sys.argv) == 2: filI = open(sys.argv[1]) for pusI in filI: pusI.strip() print "pussle:\n",pusI timStart = datetime.now() r(pusI) # &lt;- Calling the recursive solver ... timEnd = datetime.now() print "Duration (h:mm:ss.dddddd): "+str(timEnd-timStart) else: print str(len(sys.argv)) print 'Usage: python sudoku.py puzzle' </code></pre> <p>The program needs to be called with a file. That file should hold 1 sudoku per line.</p> <p>For testing I used this:</p> <pre><code>25...1........8.6...3...4.1..48.6.9...9.4.8...1..29.4.9.53.7....6..5...7......... </code></pre> <p><strong>QUESTION:</strong></p> <p>I can't understand how that single 'print a' manage to break the recursive loop, before it's done. Can anyone give an explanation?</p> <p>Best regards Martin@Hvidberg.net</p> <p>Credit: I originally found the above sudoku solver code here: <a href="http://www.scottkirkwood.com/2006/07/shortest-sudoku-solver-in-python.html" rel="nofollow noreferrer">http://www.scottkirkwood.com/2006/07/shortest-sudoku-solver-in-python.html</a> it's also shown here on StackOverflow: <a href="https://stackoverflow.com/questions/201461/shortest-sudoku-solver-in-python-how-does-it-work">Shortest Sudoku Solver in Python - How does it work?</a></p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload