Skip to content

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

Merged
merged 5 commits into from
Aug 13, 2019

Conversation

adithbharadwaj
Copy link
Contributor

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.

Copy link
Member

@cclauss cclauss left a 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

if(grid[i][j] == 0):
return False

return True
Copy link
Member

@cclauss cclauss Aug 13, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OPTIONAL: Challenges:

  1. Try to do this without hardcoding 9 and without using range() like: for row in grid: for cell in row:
  2. Try making the body of this function a one-liner using nested list comprehensions.

@cclauss
Copy link
Member

cclauss commented Aug 13, 2019

Just FYI: Look at the sudoku files in pytudes to get other implementation ideas and for some really difficult puzzles https://github.com/norvig

Copy link
Contributor Author

@adithbharadwaj adithbharadwaj left a 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 :)

@@ -75,17 +86,17 @@ def sudoku(grid):

'''

if(is_completed(grid)):
if is_completed(grid):
return True
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

return grid

@cclauss
Copy link
Member

cclauss commented Aug 13, 2019

Please fix line 90


for i in range(9):
if grid[i][column] == n:
return False
Copy link
Member

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?

Copy link
Contributor Author

@adithbharadwaj adithbharadwaj left a 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.

@cclauss
Copy link
Member

cclauss commented Aug 13, 2019

Are we ready to merge?

@adithbharadwaj
Copy link
Contributor Author

Yeah we are ready to merge. I have made all the changes you suggested and also added a grid with no solution as well.

Copy link
Member

@cclauss cclauss left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice work here!!

@cclauss cclauss merged commit f3c0b13 into TheAlgorithms:master Aug 13, 2019
@adithbharadwaj
Copy link
Contributor Author

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 :)

@cclauss
Copy link
Member

cclauss commented Aug 13, 2019

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.

stokhos pushed a commit to stokhos/Python that referenced this pull request Jan 3, 2021
…s#1128)

* Added sudoku solver in backtracking

* Added sudoku solver program

* Added sudoku solver

* Added sudoku solver

* Format with black, add doctests, cleanup main
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants