-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathvalid_sudoku.py
More file actions
81 lines (74 loc) · 2.8 KB
/
valid_sudoku.py
File metadata and controls
81 lines (74 loc) · 2.8 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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
class Solution(object):
def isValidSudoku(self, board):
"""
:type board: List[List[str]]
:rtype: bool
"""
if len(board) != 9:
return False
for r in board:
if len(r) != 9:
return False
for r in board:
check_list = [0 for i in xrange(10)]
for c in r:
if c == '.':
continue
if c < '0' or c > '9':
return False
if c.isdigit():
t = int(c)
if check_list[t] != 0:
return False
else:
check_list[t] += 1
else:
return False
for i in xrange(len(board[0])):
check_list = [0 for k in xrange(10)]
for j in xrange(len(board)):
if board[j][i] == '.':
continue
if board[j][i] < '0' or board[j][i] > '9':
return False
if board[j][i].isdigit():
t = int(board[j][i])
if check_list[t] != 0:
return False
else:
check_list[t] += 1
else:
return False
for i in xrange(9):
r = i / 3
c = i % 3
check_list = [0 for x in xrange(10)]
print r, ' ', c, ' ', check_list
for j in xrange(3):
sr = r * 3 + j
for k in xrange(3):
sc = c * 3 + k
print sr, ' ', sc
if board[sr][sc] == '.':
continue
if board[sr][sc] < '0' or board[sr][sc] > '9':
return False
if board[sr][sc].isdigit():
t = int(board[sr][sc])
if check_list[t] != 0:
return False
else:
check_list[t] += 1
else:
return False
return True
s = Solution()
print s.isValidSudoku([['5', '3', '.', '.', '7', '.', '.', '.', '.'],
['6', '.', '.', '1', '9', '5', '.', '.', '.'],
['.', '9', '8', '.', '.', '.', '.', '6', '.'],
['8', '.', '.', '.', '6', '.', '.', '.', '3'],
['4', '.', '.', '8', '.', '3', '.', '.', '1'],
['7', '.', '.', '.', '2', '.', '.', '.', '6'],
['.', '6', '.', '.', '.', '.', '2', '8', '.'],
['.', '.', '.', '4', '1', '9', '.', '.', '5'],
['.', '.', '.', '.', '8', '.', '.', '7', '9']])