forked from TheAlgorithms/Python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvalidate_sudoku_board.py
101 lines (83 loc) · 3.12 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
"""
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_ROWS = 9
NUM_COLS = 9
EMPTY_CELL = "."
IS_VALID = True
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"]])
Traceback (most recent call last):
...
Exception: Number of rows must be 9.
>>> is_valid_sudoku_board(
... [["1"], ["2"], ["3"], ["4"], ["5"], ["6"], ["7"], ["8"], ["9"]]
... )
Traceback (most recent call last):
...
Exception: Number of columns must be 9.
"""
if len(sudoku_board) != NUM_ROWS:
raise Exception("Number of rows must be 9.")
for row in sudoku_board:
if len(row) != NUM_COLS:
raise Exception("Number of columns must be 9.")
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 r in range(NUM_ROWS):
for c in range(NUM_COLS):
value = sudoku_board[r][c]
if value == EMPTY_CELL:
continue
box = (r // 3, c // 3)
if (
value in row_values[r]
or value in col_values[c]
or value in box_values[box]
):
return not IS_VALID
row_values[r].add(value)
col_values[c].add(value)
box_values[box].add(value)
return IS_VALID