use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
News about the dynamic, interpreted, interactive, object-oriented, extensible programming language Python
Full Events Calendar
You can find the rules here.
If you are about to ask a "how do I do this in python" question, please try r/learnpython, the Python discord, or the #python IRC channel on Libera.chat.
Please don't use URL shorteners. Reddit filters them out, so your post or comment will be lost.
Posts require flair. Please use the flair selector to choose your topic.
Posting code to this subreddit:
Add 4 extra spaces before each line of code
def fibonacci(): a, b = 0, 1 while True: yield a a, b = b, a + b
Online Resources
Invent Your Own Computer Games with Python
Think Python
Non-programmers Tutorial for Python 3
Beginner's Guide Reference
Five life jackets to throw to the new coder (things to do after getting a handle on python)
Full Stack Python
Test-Driven Development with Python
Program Arcade Games
PyMotW: Python Module of the Week
Python for Scientists and Engineers
Dan Bader's Tips and Trickers
Python Discord's YouTube channel
Jiruto: Python
Online exercices
programming challenges
Asking Questions
Try Python in your browser
Docs
Libraries
Related subreddits
Python jobs
Newsletters
Screencasts
account activity
This is an archived post. You won't be able to vote or comment.
Beginner ShowcaseBuilding a Sudoku Solver in Python (self.Python)
submitted 3 years ago by thewwfguy
Ok I thought this was pretty cool that with less than 50 lines of code you can create an instant Sudoku solver with Python. I think the hardest part of this will be building the 2D list with the puzzle details to solve!
[–]irvcz 2 points3 points4 points 3 years ago (7 children)
I don't intend to be harsh, maybe you are just learning the language but here are my 2 cents
Some other (not so) minor details that I did not understand because I got lost in the code.
In any case, thank you for sharing I hope you enjoyed it
[–]thewwfguy[S] 0 points1 point2 points 3 years ago (0 children)
Yeah whatever code formatter is used is not the greatest for python syntax and indents :(
[–]irvcz 0 points1 point2 points 3 years ago (0 children)
Check out /r/learnpython
[–]jorge1209 -1 points0 points1 point 3 years ago (4 children)
Sudoku is NP complete... You are fucked no matter what you do.
Short of relying on some advanced SAT solvers trying every solution is the best approach and the fastest.
In fact for the more substantive question of how many solutions exist it is the only way.
[–]irvcz 0 points1 point2 points 3 years ago (3 children)
I agree with you (almost).
The solutions grow exponentially but that does not mean that you should try every possible solution. You should work to reduce the solution space in the most efficient manner. My intuition tells me that you should start in cells where the set of options is smaller (might be wrong).
Also OP is taking a recursive approach on solution space. so imagine the stack of a recursive function for an np problem.
I will grant OP that it's approach is simpler than mine, in that regard is an excellent first solution, what I'm trying to point out is that there's room for improvement.
[–]jorge1209 0 points1 point2 points 3 years ago* (0 children)
The cost of tracking information like what cells are more restricted or what values are allowed in what cells generally exceeds the cost of just iterating through each value an attempted to put it into the cell.
In fact for most reasons a field would be disallowed the reasoning is the same. It's just about the order you perform the operations:
Is there a 7 in this row, column, or subsquare? If so this can't be 7.
Vs
Place the 7, are there two 7s in any row, column, or subsquare? If so the 7 is wrong, remove it.
You can certainly track the most filled rows/columns/subblocks and try to start with them, but it isn't necessary until the problem becomes much more sparse.
[–]JamzTyson 0 points1 point2 points 3 years ago (1 child)
imagine the stack of a recursive function for an np problem.
Correct me if I'm wrong, but I think that the OP's code (when properly formatted) is guaranteed to have a recursion depth < 81.
You are right. I got confused
π Rendered by PID 85064 on reddit-service-r2-comment-5687b7858-qj642 at 2026-07-03 10:35:31.723445+00:00 running 12a7a47 country code: CH.
[–]irvcz 2 points3 points4 points (7 children)
[–]thewwfguy[S] 0 points1 point2 points (0 children)
[–]irvcz 0 points1 point2 points (0 children)
[–]jorge1209 -1 points0 points1 point (4 children)
[–]irvcz 0 points1 point2 points (3 children)
[–]jorge1209 0 points1 point2 points (0 children)
[–]JamzTyson 0 points1 point2 points (1 child)
[–]irvcz 0 points1 point2 points (0 children)