Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 4bcde0b

Browse files
committed
Added doc comment and input error-checking to Sudoku.
1 parent 516b6f2 commit 4bcde0b

File tree

1 file changed

+7
-2
lines changed

1 file changed

+7
-2
lines changed

csp.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -491,9 +491,14 @@ class Sudoku(CSP):
491491
for v in flatten(rows)])
492492

493493
def __init__(self, grid):
494-
squares = re.findall(r'\d|\.', grid)
495-
domains = dict((var, [int(ch)] if ch.isdigit() else range(1, 10))
494+
"""Build a Sudoku problem from a string representing the grid: the digits
495+
1-9 denote a filled cell, '.' or '0' an empty one; other characters are
496+
ignored."""
497+
squares = iter(re.findall(r'\d|\.', grid))
498+
domains = dict((var, [int(ch)] if ch in '123456789' else range(1, 10))
496499
for var, ch in zip(flatten(self.rows), squares))
500+
for _ in squares:
501+
raise ValueError("Not a Sudoku grid", grid) # Too many squares
497502
CSP.__init__(self, None, domains, self.neighbors,
498503
different_values_constraint)
499504

0 commit comments

Comments
 (0)