Browse files

Extract function to assign unique places

  • Loading branch information...
1 parent 6b95a06 commit b94ef419b8179d9d250dc34b3cc53c881ceec3c1 @apauley committed Mar 2, 2011
Showing with 17 additions and 9 deletions.
  1. +17 −9 sudoku.py
View
26 sudoku.py
@@ -67,15 +67,11 @@ def eliminate(values, square, digit):
values = peer_eliminate(values, square)
if values is False:
return False
- ## (2) If a unit is reduced to only one place for a digit, then put it there
- for u in units[square]:
- digit_places = [square2 for square2 in u if digit in values[square2]]
- if len(digit_places) == 0:
- return False ## Contradiction: no place for this value
- elif len(digit_places) == 1:
- # d can only be in one place in unit; assign it there
- if not assign(values, digit_places[0], digit):
- return False
+
+ values = assign_unique_place(values, square, digit)
+ if values is False:
+ return False
+
return values
def peer_eliminate(values, square):
@@ -86,6 +82,18 @@ def peer_eliminate(values, square):
return False
return values
+def assign_unique_place(values, square, digit):
+ ## (2) If a unit is reduced to only one place for a digit, then put it there
+ for u in units[square]:
+ digit_places = [square2 for square2 in u if digit in values[square2]]
+ if len(digit_places) == 0:
+ return False ## Contradiction: no place for this value
+ elif len(digit_places) == 1:
+ # digit can only be in one place in unit; assign it there
+ if not assign(values, digit_places[0], digit):
+ return False
+ return values
+
def solve(grid):
return search(parse_grid(grid))

0 comments on commit b94ef41

Please sign in to comment.