- Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathValidSoduku.py
More file actions
Latest commit
41 lines (38 loc) · 1.31 KB
/
Copy pathValidSoduku.py
File metadata and controls
41 lines (38 loc) · 1.31 KB
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
'''
sudoku:
Grid 9 * 9, each row and column have the 1-9 numbers occuring only once
each row, each column, each 3*3 subblock must all the 1-9 but unique numbers
The cells which don't have numbers are filled with '.'
given a solution of sudoku, check whether it is valid
'''
classSudoku(object):
def__init__(self):
self.board_rows= [['.'foriinrange(9)] forjinrange(9)]
defisSolved(self):
'''Check whether the solution is valid sudoku
for each row, each column, each 3*3 block, they only have 1-9 unique numbers
with only appear once
Returns:
True or False whether the solution is valid sudoku
'''
validSet=set(['1','2','3','4','5','6','7','8','9'])
# check all the rows
forrowinself.board_rows:
ifset(row) !=validSet:
returnFalse
# check all the columns
forcinrange(9):
# column = [self.board_rows[r][c] for r in range(9)]
ifset([row[c] forrowinself.board_rows]) !=validSet:
returnFalse
# check all the blocks
# index = [0,3,6]
foriinrange(3):
forjinrange(3):
block= []
forplusiinrange(3):
forplusjinrange(3):
block.append(self.board_rows[3*i+plusi][3*j+plusj])
ifset(block) !=validSet:
returnFalse
returnTrue