-
Notifications
You must be signed in to change notification settings - Fork 2
/
sudoku.html
255 lines (234 loc) · 12 KB
/
sudoku.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>A sudoku solver — bits v0.8 documentation</title>
<link rel="stylesheet" href="_static/sphinxdoc.css" type="text/css" />
<link rel="stylesheet" href="_static/pygments.css" type="text/css" />
<script type="text/javascript">
var DOCUMENTATION_OPTIONS = {
URL_ROOT: '',
VERSION: '0.8',
COLLAPSE_INDEX: false,
FILE_SUFFIX: '.html',
HAS_SOURCE: true
};
</script>
<script type="text/javascript" src="_static/jquery.js"></script>
<script type="text/javascript" src="_static/underscore.js"></script>
<script type="text/javascript" src="_static/doctools.js"></script>
<link rel="top" title="bits v0.8 documentation" href="index.html" />
<link rel="next" title="Manipulating bitfields in Python (in most language actually)" href="bitfield.html" />
<link rel="prev" title="An update of the IMAP client example with notifications" href="imap_idle/twisted_imap/imap4client_notif.html" />
</head>
<body>
<div class="related">
<h3>Navigation</h3>
<ul>
<li class="right" style="margin-right: 10px">
<a href="genindex.html" title="General Index"
accesskey="I">index</a></li>
<li class="right" >
<a href="py-modindex.html" title="Python Module Index"
>modules</a> |</li>
<li class="right" >
<a href="bitfield.html" title="Manipulating bitfields in Python (in most language actually)"
accesskey="N">next</a> |</li>
<li class="right" >
<a href="imap_idle/twisted_imap/imap4client_notif.html" title="An update of the IMAP client example with notifications"
accesskey="P">previous</a> |</li>
<li><a href="index.html">bits v0.8 documentation</a> »</li>
</ul>
</div>
<div class="sphinxsidebar">
<div class="sphinxsidebarwrapper">
<h4>Previous topic</h4>
<p class="topless"><a href="imap_idle/twisted_imap/imap4client_notif.html"
title="previous chapter">An update of the IMAP client example with notifications</a></p>
<h4>Next topic</h4>
<p class="topless"><a href="bitfield.html"
title="next chapter">Manipulating bitfields in Python (in most language actually)</a></p>
<h3>This Page</h3>
<ul class="this-page-menu">
<li><a href="_sources/sudoku.txt"
rel="nofollow">Show Source</a></li>
</ul>
<div id="searchbox" style="display: none">
<h3>Quick search</h3>
<form class="search" action="search.html" method="get">
<input type="text" name="q" size="18" />
<input type="submit" value="Go" />
<input type="hidden" name="check_keywords" value="yes" />
<input type="hidden" name="area" value="default" />
</form>
<p class="searchtip" style="font-size: 90%">
Enter search terms or a module, class or function name.
</p>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
</div>
</div>
<div class="document">
<div class="documentwrapper">
<div class="bodywrapper">
<div class="body">
<div class="section" id="a-sudoku-solver">
<h1>A sudoku solver<a class="headerlink" href="#a-sudoku-solver" title="Permalink to this headline">¶</a></h1>
<p>This module can read the simple representation of a sudoku found in the
newspaper and display the solution. Ex:</p>
<div class="highlight-python"><pre>~$ python sudoku.py
The problem:
6 7 4 3
9 6 2
5 3 4 6
7 4 1
8 9 3 4
1 5 7
2 6 3 5
3 2 8
4 5 7 2
A solution:
9 2 6 5 1 7 4 8 3
3 7 4 9 8 6 5 2 1
5 8 1 3 2 4 7 9 6
7 4 3 8 6 5 9 1 2
8 5 9 1 7 2 3 6 4
6 1 2 4 3 9 8 5 7
2 9 8 6 4 3 1 7 5
1 3 7 2 5 8 6 4 9
4 6 5 7 9 1 2 3 8</pre>
</div>
<p>As a human brain usually scans the sudoku board and eventually
<strong>deduces</strong> the right digit in the slots, the algorithm presented
below operates in a different way. The algorithm below starts from the
top left slot and <strong>stacks up assumptions</strong> for which digit is put in
a slot, for each slot all the way to the bottom right slot. Eventually
backtrack to a previous slot to try a different assumption when no
number can be set for the current slot. On the contrary to the human
brain, the algorithm does not do deductions but can easily remember a
full stack of assumptions, which is something hard to do for the human
brain.</p>
<span class="target" id="module-sudoku"></span><p>The <em>sudoku</em> module offers three objects building a sudoku solver:</p>
<ul class="simple">
<li>the <em>Sudoku</em> class modelling the sudoku board and sudoku rules,</li>
<li>the <em>stack_assumptions</em> generic backtracking algorithm. The function
takes a list of generator functions as argument,</li>
<li>the <em>make_generators</em> function returning a list of
generator functions suited for manipulating a sudoku and compatible
with the bactracking algorithm.</li>
</ul>
<dl class="class">
<dt id="sudoku.Sudoku">
<em class="property">class </em><tt class="descclassname">sudoku.</tt><tt class="descname">Sudoku</tt><big>(</big><em>problem</em><big>)</big><a class="reference internal" href="_modules/sudoku.html#Sudoku"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#sudoku.Sudoku" title="Permalink to this definition">¶</a></dt>
<dd><p>The <em>Sudoku</em> board class has the methods for reading the start
state of a sudoku board, for representing a board. It also has the
methods for setting and freeing a digit in a slot of the board,
according to the rules of the sudoku game.</p>
<dl class="attribute">
<dt id="sudoku.Sudoku.board">
<tt class="descname">board</tt><a class="headerlink" href="#sudoku.Sudoku.board" title="Permalink to this definition">¶</a></dt>
<dd><p>A 9x9 matrix which contains the digits on the sudoku board. An
empty position is represented by a False value</p>
</dd></dl>
<dl class="method">
<dt id="sudoku.Sudoku.candidates">
<tt class="descname">candidates</tt><big>(</big><em>col</em>, <em>row</em><big>)</big><a class="reference internal" href="_modules/sudoku.html#Sudoku.candidates"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#sudoku.Sudoku.candidates" title="Permalink to this definition">¶</a></dt>
<dd><p>Returns the list of possible values for the slot specified by
the arguments, according to the current state of the sudoku
board and according to the rules of the sudoku game.</p>
<p>The sudoku rules states that the candidates are the numbers
which are not present neither in the column <em>col</em>, neither in
the line <em>row</em>, neither in the square identified by <em>col</em> and
<em>row</em>.</p>
</dd></dl>
<dl class="method">
<dt id="sudoku.Sudoku.attempt">
<tt class="descname">attempt</tt><big>(</big><em>*args</em>, <em>**kwds</em><big>)</big><a class="reference internal" href="_modules/sudoku.html#Sudoku.attempt"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#sudoku.Sudoku.attempt" title="Permalink to this definition">¶</a></dt>
<dd><p>A context manager which sets the value of the board at
position: <em>col</em>, <em>line</em> on entering the context and which
frees the position on exiting the context.</p>
</dd></dl>
</dd></dl>
<dl class="function">
<dt id="sudoku.stack_assumptions">
<tt class="descclassname">sudoku.</tt><tt class="descname">stack_assumptions</tt><big>(</big><em>generators</em>, <em>i=0</em><big>)</big><a class="reference internal" href="_modules/sudoku.html#stack_assumptions"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#sudoku.stack_assumptions" title="Permalink to this definition">¶</a></dt>
<dd><p>Takes a list of generators. This list is assumed to manipulate
a shared representation of the problem. When this algorithm
yields, a solution has been found and can be printed.</p>
<p>The algorithm works by calling the generator at the <em>nth</em> position
of the list, and pulls the <em>next()</em> method on the iterator
returned:</p>
<ol class="arabic simple">
<li>either <em>next()</em> returns, in which case, the algorithm
instantiates the generator from position <strong>n+1</strong> of the input
list function and tries to pull its <em>next()</em> method,</li>
<li>or the method raises a StopIteration, in which case, the
algorithm trigger <em>next()</em> on the generator at position <strong>n-1</strong>,</li>
</ol>
<p>This algorithm yields whenever every generator of the list has
yielded, at this point, every position of the board is filled with
a digit according to the sudoku rules: a solution has been
reached and the board can be printed.</p>
<p>When a generator has yielded, this means that a suitable candidate
could be found and was set in the board’s slot and that an
assumption can be tried on the next slot, with generator i+1.</p>
<p>When a generator raises a StopIteration, then a dead-end was
met. A wrong assumption must have been taken somewhere along the
stack of the previous recursion: the algorithm backtracks at the
previous recursion, another assumption can be attempted.</p>
</dd></dl>
<p>It is interesting to note that the backtracking algorithm (the
algorithm whichis able to stack assumptions) really is independant
from the sudoku rules. The algorithm requires an argument with a
precise interface and blindly triggers them, whether the argument is
manipulating a sudoku, the eight queens or the knight problem. When
the algorithm yields, the sudoku board or chessboard manipulated by
the argument has cooked a solution.</p>
<dl class="function">
<dt id="sudoku.make_generators">
<tt class="descclassname">sudoku.</tt><tt class="descname">make_generators</tt><big>(</big><em>sudoku</em><big>)</big><a class="reference internal" href="_modules/sudoku.html#make_generators"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#sudoku.make_generators" title="Permalink to this definition">¶</a></dt>
<dd><p>Returns a list of candidate generators for use with the
backtrack algorithm stack_assumptions. The sudoku argument must
provide two functions: <em>candidates(i,j)</em>, and <em>attempt(col, row,
candidate)</em> and a member attribute called <em>board</em>, which is a 9x9
matrix.</p>
<p>There are as many generator functions than there are slots on the
sudoku board, they are stored in a list. Each generator function
is specific to a slot: it actually <em>contains</em> the coordinates of
the slot, like a closure.</p>
<p>When called for the first time, the generator computes the list of
candidate numbers for the slot, according to the current sudoku
board. The list of candidates depends on the state of the board at
the time the generator is called for the first time.</p>
</dd></dl>
</div>
</div>
</div>
</div>
<div class="clearer"></div>
</div>
<div class="related">
<h3>Navigation</h3>
<ul>
<li class="right" style="margin-right: 10px">
<a href="genindex.html" title="General Index"
>index</a></li>
<li class="right" >
<a href="py-modindex.html" title="Python Module Index"
>modules</a> |</li>
<li class="right" >
<a href="bitfield.html" title="Manipulating bitfields in Python (in most language actually)"
>next</a> |</li>
<li class="right" >
<a href="imap_idle/twisted_imap/imap4client_notif.html" title="An update of the IMAP client example with notifications"
>previous</a> |</li>
<li><a href="index.html">bits v0.8 documentation</a> »</li>
</ul>
</div>
<div class="footer">
© Copyright 2009, Jean Daniel Browne.
Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 1.0.4.
</div>
</body>
</html>