-
-
Notifications
You must be signed in to change notification settings - Fork 47k
Create Sudoku_Solver.py #10619
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
Create Sudoku_Solver.py #10619
Changes from all commits
5a8be7c
5d9209c
5bba168
3a0970c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
class Solution: | ||
def solveSudoku(self, board: List[List[str]]) -> None: | ||
n = 9 | ||
|
||
def isValid(row, col, ch): | ||
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. As there is no test file in this pull request nor any test function or class in the file Please provide return type hint for the function: Variable and function names should follow the Please provide type hint for the parameter: Please provide type hint for the parameter: Please provide type hint for the parameter: |
||
row, col = int(row), int(col) | ||
|
||
for i in range(9): | ||
if board[i][col] == ch: | ||
return False | ||
if board[row][i] == ch: | ||
return False | ||
|
||
if board[3 * (row // 3) + i // 3][3 * (col // 3) + i % 3] == ch: | ||
return False | ||
|
||
return True | ||
|
||
def solve(row, col): | ||
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. As there is no test file in this pull request nor any test function or class in the file Please provide return type hint for the function: Please provide type hint for the parameter: Please provide type hint for the parameter: |
||
if row == n: | ||
return True | ||
if col == n: | ||
return solve(row + 1, 0) | ||
|
||
if board[row][col] == ".": | ||
for i in range(1, 10): | ||
if isValid(row, col, str(i)): | ||
board[row][col] = str(i) | ||
|
||
if solve(row, col + 1): | ||
return True | ||
else: | ||
board[row][col] = "." | ||
return False | ||
else: | ||
return solve(row, col + 1) | ||
|
||
solve(0, 0) | ||
|
||
|
||
# do upvote if it helps. |
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.
As there is no test file in this pull request nor any test function or class in the file
data_structures/arrays/sudoku_solver.py
, please provide doctest for the functionsolveSudoku
Variable and function names should follow the
snake_case
naming convention. Please update the following name accordingly:solveSudoku