99
1010
1111def cross (items_a , items_b ):
12- "Cross product of elements in A and elements in B."
12+ """
13+ Cross product of elements in A and elements in B.
14+ """
1315 return [a + b for a in items_a for b in items_b ]
1416
1517
@@ -27,7 +29,7 @@ def cross(items_a, items_b):
2729
2830
2931def test ():
30- "A set of unit tests."
32+ """ A set of unit tests."" "
3133 assert len (squares ) == 81
3234 assert len (unitlist ) == 27
3335 assert all (len (units [s ]) == 3 for s in squares )
@@ -47,8 +49,10 @@ def test():
4749
4850
4951def parse_grid (grid ):
50- """Convert grid to a dict of possible values, {square: digits}, or
51- return False if a contradiction is detected."""
52+ """
53+ Convert grid to a dict of possible values, {square: digits}, or
54+ return False if a contradiction is detected.
55+ """
5256 ## To start, every square can be any digit; then assign values from the grid.
5357 values = {s : digits for s in squares }
5458 for s , d in grid_values (grid ).items ():
@@ -58,15 +62,19 @@ def parse_grid(grid):
5862
5963
6064def grid_values (grid ):
61- "Convert grid into a dict of {square: char} with '0' or '.' for empties."
65+ """
66+ Convert grid into a dict of {square: char} with '0' or '.' for empties.
67+ """
6268 chars = [c for c in grid if c in digits or c in "0." ]
6369 assert len (chars ) == 81
6470 return dict (zip (squares , chars ))
6571
6672
6773def assign (values , s , d ):
68- """Eliminate all the other values (except d) from values[s] and propagate.
69- Return values, except return False if a contradiction is detected."""
74+ """
75+ Eliminate all the other values (except d) from values[s] and propagate.
76+ Return values, except return False if a contradiction is detected.
77+ """
7078 other_values = values [s ].replace (d , "" )
7179 if all (eliminate (values , s , d2 ) for d2 in other_values ):
7280 return values
@@ -75,8 +83,10 @@ def assign(values, s, d):
7583
7684
7785def eliminate (values , s , d ):
78- """Eliminate d from values[s]; propagate when values or places <= 2.
79- Return values, except return False if a contradiction is detected."""
86+ """
87+ Eliminate d from values[s]; propagate when values or places <= 2.
88+ Return values, except return False if a contradiction is detected.
89+ """
8090 if d not in values [s ]:
8191 return values ## Already eliminated
8292 values [s ] = values [s ].replace (d , "" )
@@ -99,7 +109,9 @@ def eliminate(values, s, d):
99109
100110
101111def display (values ):
102- "Display these values as a 2-D grid."
112+ """
113+ Display these values as a 2-D grid.
114+ """
103115 width = 1 + max (len (values [s ]) for s in squares )
104116 line = "+" .join (["-" * (width * 3 )] * 3 )
105117 for r in rows :
@@ -114,19 +126,24 @@ def display(values):
114126
115127
116128def solve (grid ):
129+ """
130+ Solve the grid.
131+ """
117132 return search (parse_grid (grid ))
118133
119134
120135def some (seq ):
121- "Return some element of seq that is true."
136+ """ Return some element of seq that is true."" "
122137 for e in seq :
123138 if e :
124139 return e
125140 return False
126141
127142
128143def search (values ):
129- "Using depth-first search and propagation, try all possible values."
144+ """
145+ Using depth-first search and propagation, try all possible values.
146+ """
130147 if values is False :
131148 return False ## Failed earlier
132149 if all (len (values [s ]) == 1 for s in squares ):
@@ -137,9 +154,11 @@ def search(values):
137154
138155
139156def solve_all (grids , name = "" , showif = 0.0 ):
140- """Attempt to solve a sequence of grids. Report results.
157+ """
158+ Attempt to solve a sequence of grids. Report results.
141159 When showif is a number of seconds, display puzzles that take longer.
142- When showif is None, don't display any puzzles."""
160+ When showif is None, don't display any puzzles.
161+ """
143162
144163 def time_solve (grid ):
145164 start = time .monotonic ()
@@ -162,7 +181,9 @@ def time_solve(grid):
162181
163182
164183def solved (values ):
165- "A puzzle is solved if each unit is a permutation of the digits 1 to 9."
184+ """
185+ A puzzle is solved if each unit is a permutation of the digits 1 to 9.
186+ """
166187
167188 def unitsolved (unit ):
168189 return {values [s ] for s in unit } == set (digits )
@@ -177,9 +198,11 @@ def from_file(filename, sep="\n"):
177198
178199
179200def random_puzzle (assignments = 17 ):
180- """Make a random puzzle with N or more assignments. Restart on contradictions.
201+ """
202+ Make a random puzzle with N or more assignments. Restart on contradictions.
181203 Note the resulting puzzle is not guaranteed to be solvable, but empirically
182- about 99.8% of them are solvable. Some have multiple solutions."""
204+ about 99.8% of them are solvable. Some have multiple solutions.
205+ """
183206 values = {s : digits for s in squares }
184207 for s in shuffled (squares ):
185208 if not assign (values , s , random .choice (values [s ])):
@@ -191,7 +214,9 @@ def random_puzzle(assignments=17):
191214
192215
193216def shuffled (seq ):
194- "Return a randomly shuffled copy of the input sequence."
217+ """
218+ Return a randomly shuffled copy of the input sequence.
219+ """
195220 seq = list (seq )
196221 random .shuffle (seq )
197222 return seq
0 commit comments