-
-
Notifications
You must be signed in to change notification settings - Fork 46.9k
Optimize and_gate and nand_gate #10591
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
cclauss
merged 6 commits into
TheAlgorithms:master
from
SandeepaDilshanAlagiyawanna:0x3psilon
Oct 16, 2023
Merged
Changes from 2 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
b74653e
Added more optimized sudoku solver algorithm
SandeepaDilshanAlagiyawanna 5a66a76
Added more optimized sudoku solver algorithm and File Renamed
SandeepaDilshanAlagiyawanna 76c5a58
and_gate is Optimized
SandeepaDilshanAlagiyawanna 48e0d90
and_gate is Optimized
SandeepaDilshanAlagiyawanna 8ea8792
and_gate is Optimized
SandeepaDilshanAlagiyawanna 7538d74
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 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 hidden or 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,109 @@ | ||
def is_valid(board, row, col, num): | ||
""" Checks if the given number is valid for the given cell in the Sudoku board """ | ||
for i in range(9): | ||
if ( | ||
# checking is there any same number in row | ||
board[row][i] == num or | ||
# checking is there any same number in column | ||
board[i][col] == num or | ||
# checking is there any same number in cell(3*3) | ||
board[(row // 3) * 3 + i // 3][(col // 3) * 3 + i % 3] == num | ||
): | ||
# if so return false | ||
return False | ||
return True | ||
|
||
|
||
"""Taking the Board and checking the availability""" | ||
|
||
|
||
def solve_sudoku(board): | ||
cclauss marked this conversation as resolved.
Show resolved
Hide resolved
|
||
# use of recursive function solve. | ||
def solve(row, col): | ||
cclauss marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
# if row number is 9, return true as there is no any other row (last Row Solved) | ||
if row == 9: | ||
return True | ||
|
||
# if column number is 9, then we should change the row and check again | ||
if col == 9: | ||
# resetting row number to next and column number to 0 | ||
return solve(row + 1, 0) | ||
|
||
# if the number of the board[row][col] is not the 0, then it is solved, then we should move o next. | ||
if board[row][col] != 0: | ||
# moving to next column | ||
return solve(row, col + 1) | ||
|
||
# setting a number between 1-9 | ||
for num in range(1, 10): | ||
|
||
# checking the availability of that number | ||
if is_valid(board, row, col, num): | ||
|
||
# if the number is ok,then setting the number | ||
board[row][col] = num | ||
|
||
# moving to next column | ||
if solve(row, col + 1): | ||
return True | ||
|
||
# if none above are not fitting, lets hold the value of cell as zero, and then we can call it again | ||
board[row][col] = 0 | ||
|
||
# returning none as algorithm cannot find any solution | ||
return False | ||
|
||
# starting with (0,0) cell | ||
if solve(0, 0): | ||
|
||
# return the solution | ||
return board | ||
else: | ||
|
||
# no solution | ||
return None | ||
|
||
|
||
""" printing the board """ | ||
|
||
|
||
def print_board(board): | ||
cclauss marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if board: | ||
|
||
# printing the board row by row | ||
for row in board: | ||
|
||
# joining the int as strings by including space in between | ||
print(" ".join(map(str, row))) | ||
else: | ||
|
||
print("No solution found.") | ||
|
||
|
||
def main(): | ||
cclauss marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
board = [ | ||
[0, 0, 3, 0, 0, 0, 0, 0, 0], | ||
[0, 4, 0, 2, 0, 0, 6, 3, 0], | ||
[0, 8, 2, 1, 0, 0, 0, 0, 9], | ||
[0, 0, 0, 4, 0, 6, 0, 0, 0], | ||
[5, 0, 0, 0, 9, 0, 1, 0, 0], | ||
[3, 0, 0, 0, 0, 0, 0, 0, 0], | ||
[0, 0, 0, 0, 0, 0, 7, 8, 0], | ||
[0, 0, 0, 9, 0, 0, 0, 6, 0], | ||
[0, 0, 1, 7, 2, 0, 4, 0, 0] | ||
] | ||
|
||
# taking the solution board | ||
solution = solve_sudoku(board) | ||
|
||
if solution: | ||
print("The solved Sudoku puzzle:") | ||
print_board(solution) | ||
else: | ||
print("No solution found.") | ||
|
||
|
||
if __name__ == "__main__": | ||
main() |
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.
Please provide return type hint for the function:
is_valid
. If the function does not return a value, please provide the type hint as:def function() -> None:
As there is no test file in this pull request nor any test function or class in the file
backtracking/sucoku_solver_optimized.py
, please provide doctest for the functionis_valid
Please provide type hint for the parameter:
board
Please provide type hint for the parameter:
row
Please provide type hint for the parameter:
col
Please provide type hint for the parameter:
num