Skip to content
Browse files

Replace some() with first_valid_result() to better comply with the

Erlang version. Start counting the number of eliminations.
  • Loading branch information...
1 parent 994152d commit 09c41ef8c14b1bdba19fc1a2dd5f8a07ac78b43d @apauley committed
Showing with 12 additions and 8 deletions.
  1. +12 −8 sudoku.py
View
20 sudoku.py
@@ -79,6 +79,7 @@ def eliminate(puzzle, square, digit):
if len(puzzle[square]) == 0:
return failed(puzzle) ## Contradiction: removed last value
+ puzzle.count += 1
puzzle = peer_eliminate(puzzle, square)
if has_failed(puzzle):
return failed(puzzle)
@@ -121,14 +122,17 @@ def search(puzzle):
## Choose the unfilled square s with the fewest possibilities
possibilities, square = min((len(puzzle[s]), s)
for s in squares if len(puzzle[s]) > 1)
- return some(search(assign(puzzle.copy(), square, digit))
- for digit in puzzle[square])
-
-def some(seq):
- "Return some element of seq that is true."
- for e in seq:
- if e: return e
- return failed(e)
+ return first_valid_result(puzzle, square, puzzle[square])
+
+def first_valid_result(puzzle, square, values):
+ if len(values) == 0:
+ return failed(puzzle)
+ new_puzzle = search(assign(puzzle.copy(), square, values[0]))
+ if new_puzzle:
+ return new_puzzle
+ diff = new_puzzle.count - puzzle.count
+ puzzle.count += diff
+ return first_valid_result(puzzle, square, values[1:])
def solve_all(grids, name='', showif=0.0):
"""Attempt to solve a sequence of grids. Report results.

0 comments on commit 09c41ef

Please sign in to comment.
Something went wrong with that request. Please try again.