forked from TheAlgorithms/Python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
validate_sudoku_board.py
167 lines (147 loc) · 5.72 KB
/
validate_sudoku_board.py
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
"""
LeetCode 36. Valid Sudoku
https://leetcode.com/problems/valid-sudoku/
https://en.wikipedia.org/wiki/Sudoku
Determine if a 9 x 9 Sudoku board is valid. Only the filled cells need to be
validated according to the following rules:
- Each row must contain the digits 1-9 without repetition.
- Each column must contain the digits 1-9 without repetition.
- Each of the nine 3 x 3 sub-boxes of the grid must contain the digits 1-9
without repetition.
Note:
A Sudoku board (partially filled) could be valid but is not necessarily
solvable.
Only the filled cells need to be validated according to the mentioned rules.
"""
from collections import defaultdict
NUM_SQUARES = 9
EMPTY_CELL = "."
def is_valid_sudoku_board(sudoku_board: list[list[str]]) -> bool:
"""
This function validates (but does not solve) a sudoku board.
The board may be valid but unsolvable.
>>> is_valid_sudoku_board([
... ["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"]
... ])
True
>>> is_valid_sudoku_board([
... ["8","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"]
... ])
False
>>> is_valid_sudoku_board([
... ["1","2","3","4","5","6","7","8","9"]
... ,["4","5","6","7","8","9","1","2","3"]
... ,["7","8","9","1","2","3","4","5","6"]
... ,[".",".",".",".",".",".",".",".","."]
... ,[".",".",".",".",".",".",".",".","."]
... ,[".",".",".",".",".",".",".",".","."]
... ,[".",".",".",".",".",".",".",".","."]
... ,[".",".",".",".",".",".",".",".","."]
... ,[".",".",".",".",".",".",".",".","."]
... ])
True
>>> is_valid_sudoku_board([
... ["1","2","3",".",".",".",".",".","."]
... ,["4","5","6",".",".",".",".",".","."]
... ,["7","8","9",".",".",".",".",".","."]
... ,[".",".",".","4","5","6",".",".","."]
... ,[".",".",".","7","8","9",".",".","."]
... ,[".",".",".","1","2","3",".",".","."]
... ,[".",".",".",".",".",".","7","8","9"]
... ,[".",".",".",".",".",".","1","2","3"]
... ,[".",".",".",".",".",".","4","5","6"]
... ])
True
>>> is_valid_sudoku_board([
... ["1","2","3",".",".",".","5","6","4"]
... ,["4","5","6",".",".",".","8","9","7"]
... ,["7","8","9",".",".",".","2","3","1"]
... ,[".",".",".","4","5","6",".",".","."]
... ,[".",".",".","7","8","9",".",".","."]
... ,[".",".",".","1","2","3",".",".","."]
... ,["3","1","2",".",".",".","7","8","9"]
... ,["6","4","5",".",".",".","1","2","3"]
... ,["9","7","8",".",".",".","4","5","6"]
... ])
True
>>> is_valid_sudoku_board([
... ["1","2","3","4","5","6","7","8","9"]
... ,["2",".",".",".",".",".",".",".","8"]
... ,["3",".",".",".",".",".",".",".","7"]
... ,["4",".",".",".",".",".",".",".","6"]
... ,["5",".",".",".",".",".",".",".","5"]
... ,["6",".",".",".",".",".",".",".","4"]
... ,["7",".",".",".",".",".",".",".","3"]
... ,["8",".",".",".",".",".",".",".","2"]
... ,["9","8","7","6","5","4","3","2","1"]
... ])
False
>>> is_valid_sudoku_board([
... ["1","2","3","8","9","7","5","6","4"]
... ,["4","5","6","2","3","1","8","9","7"]
... ,["7","8","9","5","6","4","2","3","1"]
... ,["2","3","1","4","5","6","9","7","8"]
... ,["5","6","4","7","8","9","3","1","2"]
... ,["8","9","7","1","2","3","6","4","5"]
... ,["3","1","2","6","4","5","7","8","9"]
... ,["6","4","5","9","7","8","1","2","3"]
... ,["9","7","8","3","1","2","4","5","6"]
... ])
True
>>> is_valid_sudoku_board([["1", "2", "3", "4", "5", "6", "7", "8", "9"]])
Traceback (most recent call last):
...
ValueError: Sudoku boards must be 9x9 squares.
>>> is_valid_sudoku_board(
... [["1"], ["2"], ["3"], ["4"], ["5"], ["6"], ["7"], ["8"], ["9"]]
... )
Traceback (most recent call last):
...
ValueError: Sudoku boards must be 9x9 squares.
"""
if len(sudoku_board) != NUM_SQUARES or (
any(len(row) != NUM_SQUARES for row in sudoku_board)
):
error_message = f"Sudoku boards must be {NUM_SQUARES}x{NUM_SQUARES} squares."
raise ValueError(error_message)
row_values: defaultdict[int, set[str]] = defaultdict(set)
col_values: defaultdict[int, set[str]] = defaultdict(set)
box_values: defaultdict[tuple[int, int], set[str]] = defaultdict(set)
for row in range(NUM_SQUARES):
for col in range(NUM_SQUARES):
value = sudoku_board[row][col]
if value == EMPTY_CELL:
continue
box = (row // 3, col // 3)
if (
value in row_values[row]
or value in col_values[col]
or value in box_values[box]
):
return False
row_values[row].add(value)
col_values[col].add(value)
box_values[box].add(value)
return True
if __name__ == "__main__":
from doctest import testmod
from timeit import timeit
testmod()
print(timeit("is_valid_sudoku_board(valid_board)", globals=globals()))
print(timeit("is_valid_sudoku_board(invalid_board)", globals=globals()))