-
-
Notifications
You must be signed in to change notification settings - Fork 46.6k
Added validate sudoku board function #9881
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 15 commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
4c8450f
Added algorithm to deeply clone a graph
dbring 0d1134d
Fixed file name and removed a function call
dbring 712c35e
Removed nested function and fixed class parameter types
dbring ebf333a
Fixed doctests
dbring bfe956b
bug fix
dbring 1552e6f
Added class decorator
dbring 01b4473
Updated doctests and fixed precommit errors
dbring 88f73a5
Cleaned up code
dbring 20f5565
Simplified doctest
dbring 3d92f87
Added doctests
dbring d4ffda8
Code simplification
dbring 37f666e
Created function which validates sudoku boards
dbring 7a601f5
Update matrix/validate_sudoku_board.py
cclauss 1e9621b
Fixed precommit errors
dbring 06bfdbc
Removed file accidentally included
dbring d5a3389
Improved readability and simplicity
dbring 56bbe1f
Add timeit benchmarks
cclauss 0dae81f
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] dc9ae32
Update validate_sudoku_board.py
cclauss File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,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.") | ||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||||||||||
|
||||||||||||||||||||||
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): | ||||||||||||||||||||||
cclauss marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||||||||||
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 |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's use NUM_SQUARES instead of NUM_ROWS and NUM_ROWS.
Lets True and False in a boolean function.