-
-
Notifications
You must be signed in to change notification settings - Fork 46.6k
Added sudoku solving program in backtracking algorithms #1128
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
Added sudoku solving program in backtracking algorithms #1128
Conversation
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.
This looks really good. Thanks for your contribution. A few ideas for you to consider...
"Sudoku is a denial of service attack on the human intellect." https://norvig.com/sudoku.html
backtracking/sudoku.py
Outdated
if(grid[i][j] == 0): | ||
return False | ||
|
||
return True |
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.
OPTIONAL: Challenges:
Try to do this without hardcoding 9 and without using range() like: for row in grid: for cell in row:- Try making the body of this function a one-liner using nested list comprehensions.
Just FYI: Look at the sudoku files in pytudes to get other implementation ideas and for some really difficult puzzles https://github.com/norvig |
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.
I have made the changes that you suggested. Please review the pull request :)
backtracking/sudoku.py
Outdated
@@ -75,17 +86,17 @@ def sudoku(grid): | |||
|
|||
''' | |||
|
|||
if(is_completed(grid)): | |||
if is_completed(grid): | |||
return True |
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.
return grid
Please fix line 90 |
backtracking/sudoku.py
Outdated
|
||
for i in range(9): | ||
if grid[i][column] == n: | ||
return False |
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.
OPTIONAL: Would it be possible to compress lines 46-52 onto just three lines?
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.
I now return the completed grid in the function.
Are we ready to merge? |
Yeah we are ready to merge. I have made all the changes you suggested and also added a grid with no solution as well. |
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.
Nice work here!!
Thanks a lot, sir! I am a beginner to open source and would love to contribute more to the repository. Any guidance or suggestions are much appreciated. Thanks again :) |
https://github.com/TheAlgorithms/Python/blob/master/backtracking/sudoku.py#L97 To run these doctest locally, do python3 -m doctest -v backtracking/sudoku.py https://github.com/TheAlgorithms/Python/issues/created_by/cclauss might be good ways to get more involved. |
…s#1128) * Added sudoku solver in backtracking * Added sudoku solver program * Added sudoku solver * Added sudoku solver * Format with black, add doctests, cleanup main
I have added sudoku.py which is a program to solve the famous sudoku puzzle in python. Please review it and suggest improvements/comments, if any.