No video

Create Sudoku Solver with Python in 20 minutes

  Рет қаралды 65,413

Python enthusiast

Python enthusiast

Күн бұрын

Support the channel via the links below:
Paypal: www.paypal.com...
www.buymeacoff...
The goal of this tutorial is to explain step by step how a Sudoku solver is created with Python and what the logic behind each step is.
Github link: github.com/kos...

Пікірлер: 102
@bluegodofspeed
@bluegodofspeed 3 жыл бұрын
Bro I've been wanting to do one of these projects to add to my resume, and I've watched a bunch of videos on the same topic. Yours is the clearest, most detailed, best video I've found. You explain every detail, take us step-by-step, is logical and not boring. Others type the same code but idk what they're saying. Thank you so much for your video. You explain perfectly // division to isolate the squares, you explain perfectly the column[i] to check row, column etc. It's perfect, thank you so much. You explain things very well.
@Pythonenthusiast
@Pythonenthusiast 3 жыл бұрын
Thanks a lot for the kind words, it means a lot to me!
@alitoto5452
@alitoto5452 Жыл бұрын
absolute legend if you are unsure to watch this video or another one then watch this one 100% best explanation ever
@Pythonenthusiast
@Pythonenthusiast Жыл бұрын
I am glad it was helpful!
@reiherentenorbert7487
@reiherentenorbert7487 3 жыл бұрын
Very elegantly solved! - I once wrote a solver myself, wrote much more code and did only find one solution. Excellent!
@Pythonenthusiast
@Pythonenthusiast 3 жыл бұрын
Thanks for the comment! We all get better over time if we put the hours into learning :)
@riyanwanchoo9455
@riyanwanchoo9455 2 жыл бұрын
I wrote more than 200 lines of code to solve a Sudoku puzzle but it didn't even work
@josecarrera6519
@josecarrera6519 3 жыл бұрын
this is super intuitive, we are solving a sudoku program at uni and ours is so much more complicated
@Pythonenthusiast
@Pythonenthusiast 3 жыл бұрын
I am happy to read a comment such as this one :)
@M1EEA
@M1EEA 9 ай бұрын
That's the most clear explanation i have found ❤
@randyfriend
@randyfriend 2 жыл бұрын
Very nice explanation. It doesn't matter in this case, but as a FYI, // division does not round down. It truncates the remainders and only returns the integer part of the result. Knowing the difference would be important for a technical interview or a case where you were asked about Python knowledge/workings.
@Dylan-zx6ji
@Dylan-zx6ji 2 жыл бұрын
I thought that it rounds to negative infinity. If you had negative number result from // it would go to negative infinity I think.
@randyfriend
@randyfriend 2 жыл бұрын
@@Dylan-zx6ji interesting. I just did some negatives and it does round on them. Good to know.
@goutvols103
@goutvols103 3 ай бұрын
I ran your code and added lots of print statements to understand where the code backtracks. It is the embedded solve() call which backtracks to the last known good row, column and then increments the counter by one (1).
@parkuuu
@parkuuu Жыл бұрын
Recursion is so damn scary and brilliant at the same time. I can only hope to master this thinking style.
@rajat9302
@rajat9302 2 жыл бұрын
Bro, you explain this logic clearly thank you so much and keep growing
@raveenachakrapani1090
@raveenachakrapani1090 2 жыл бұрын
What a precise and clear explanation !!! Thanks a ton !!
@Pythonenthusiast
@Pythonenthusiast 2 жыл бұрын
Thanks for the kind words, great you enjoyed the tutorial!
@paytakbaba9424
@paytakbaba9424 Жыл бұрын
bro thanks for helping for my project. apart all of that you helped me to understand
@Pythonenthusiast
@Pythonenthusiast Жыл бұрын
Glad to hear that!
@ChessQuickies
@ChessQuickies 2 жыл бұрын
Tysm! This project was short and easy to understand and clear. you just got an extra like!
@Pythonenthusiast
@Pythonenthusiast 2 жыл бұрын
I am glad it was helpful!
@carlocirillo8708
@carlocirillo8708 2 жыл бұрын
very good explaination. but i don't understand only one thing: how can writing "grid[row][column]" = 0 after solve() help if it doesn't find any solution?
@aptmap5181
@aptmap5181 2 жыл бұрын
it just resets the cell back to 0, go back to the previous cell and solve with the next number in that loop. Watch the original video from Dr. Altenkirch at Computerphile not some copycats and you will understand a lot better
@saransp3588
@saransp3588 Жыл бұрын
Very good explanation ! Thank you. How we can give 'number' as user input and it will check with conditions ?
@gmax30
@gmax30 2 жыл бұрын
Amazing stuff. Could you please give more details for the part where when it's stack it goes back. How the "it goes back" translated in your code?
@krampsea1587
@krampsea1587 2 жыл бұрын
The return statement at the bottom of the loop "range(1,10)" makes it go back
@superprocz
@superprocz 2 жыл бұрын
Amazing tutorial and interesting approach, but I didn't quite understand how does the more solutions work. Does the function run again when you press enter? How does it not make the same grid as before?
@Pythonenthusiast
@Pythonenthusiast 2 жыл бұрын
Basically, it continues looping assuming the correct solution that was found was incorrect. So the correct solution is ignored and it moves on :)
@superprocz
@superprocz 2 жыл бұрын
@@Pythonenthusiast Thank you so much!! :D
@clouddrive6833
@clouddrive6833 Жыл бұрын
it was clear cut explanation ,keep posting videos
@JoseMBrito-iu6hg
@JoseMBrito-iu6hg 2 жыл бұрын
Very nice solution. I have two questions: 1) Where does the control go back to the previous cell when no number is possible for the current [row][column]? 2) How does it find other solutions? i don´t undestand how it does that after the input statement.
@Pythonenthusiast
@Pythonenthusiast 2 жыл бұрын
Hey there, below are my answers to your questions: 1. The whole point of the for loops is to continue to the next number when the solution for the current number doesn't work. Let's assume you start with the first number being 1 (first cell available, so the combination of the first row and first column). Next, you move to the next cell (row 1, column 2). The script starts again with the number 1. Well, that doesn't work as it is already there in the same row. So it continues with the number 2. Great, that number is available, then it moves forward, and so on. The moment there's no solution for a given cell with all numbers 1 to 9, it goes back and changes the number before. I hope this makes it clear. 2. There could be more than one solution for the same sudoku. The script starts with the smallest number and goes up until a solution is found. In order to find another solution, basically, the script is ignoring the found solution and continues to look for another one. Hope this makes it more clear!
@aptmap5181
@aptmap5181 2 жыл бұрын
The key is the empty return for impossible candidate numbers, not the for loop. This guy just copied almost verbatim from Dr. Altenkirch at Computerphile. He even copied the same unconventional y for row x for column convention from that video. What a fraud and he clearly doesn't know sh*t about this algorithm
@markob2571
@markob2571 Жыл бұрын
What is numpy?
@zerozeronine5461
@zerozeronine5461 3 жыл бұрын
may use set difference to reduce some codes. eg {1,2,3,4,5,6,7,8,9} - set of numbers at that row - set of numbers at that column - set of numbers at that block = only possible numbers at that position. Some people even put those numbers in one byte to perform bitwise operations ^ &.
@Pythonenthusiast
@Pythonenthusiast 3 жыл бұрын
Hi there, thanks for the comment, I fully agree with it, for sure there's room for simplifying. As the tutorial is mainly for beginners, I didn't spend time to find the most optimal solution, but the one that's easiest to explain/understand.
@amulyaratna638
@amulyaratna638 Жыл бұрын
Thanks bro for such clear explanation
@ashishzarekar9599
@ashishzarekar9599 11 ай бұрын
For me this code prints original(unsolved) grid only, Could someone help me please?
@williammusimenta1712
@williammusimenta1712 2 жыл бұрын
Hallo, thank you for the clear explanation. I have run the code. And not showing any output in the terminal and no errors. Please help
@niranjanshankaran3493
@niranjanshankaran3493 2 жыл бұрын
I did not understand how does recursion solve ()works in line after if possible () condition..without else condition how can only one resent grid[row][col]=0
@Arpitjain04
@Arpitjain04 2 жыл бұрын
have you found the answer as i am stuck at same position?
@aptmap5181
@aptmap5181 2 жыл бұрын
the empty return acts like an else statement after all 1 to 9 are deemed impossible for a particular cell. This guy did not know how to explain because he just copied from Dr. Altenkirch's Computerphile video
@pranavkochhar9352
@pranavkochhar9352 3 жыл бұрын
very well explained. Thank you so much
@Pythonenthusiast
@Pythonenthusiast 3 жыл бұрын
Thanks for the kind words!
@Lapusso650
@Lapusso650 10 ай бұрын
*does it appear Not “is it appearing”.
@Danitux11
@Danitux11 Жыл бұрын
i copied the exact code from github and it has been 2 minutes running and still didn't get the solution
@praizdev
@praizdev 2 жыл бұрын
Clearly understood though will some minor confusion like where you reset grid[row][col]=0 Please can you do a video on minimax algorithm with tictactoe
@ulrichnana9821
@ulrichnana9821 Жыл бұрын
best explanation ever thanks you
@Pythonenthusiast
@Pythonenthusiast Жыл бұрын
Glad you liked it!
@hobbyistmovement
@hobbyistmovement Жыл бұрын
Thanks a ton for this video.
@melbbb5673
@melbbb5673 2 жыл бұрын
I like your explanation!
@rabihbadr54
@rabihbadr54 Жыл бұрын
Explain why you use range(0,9) when you can use range(9)
@Pythonenthusiast
@Pythonenthusiast Жыл бұрын
These tutorials are for beginners and I want to point out that the range starts from 0 and goes up to 9. You are absolutely right that range(9) can be used.
@ahmednasser6757
@ahmednasser6757 2 жыл бұрын
why it is for `number in range(1, 10)` and not for `number in range(1, 9) ` ??
@Pythonenthusiast
@Pythonenthusiast 2 жыл бұрын
If you use range(5), the output would be 0,1,2,3,4 (output is total of 5, but starting with 0, so it goes up to 5, but doesn't include that number). In this example, the starting point should not be 0 but 1. So, the range selected it (1,10), which means, start with 1 and go all the way up to 10, but doesn't include 10. Hope this helps!
@rowdy7664
@rowdy7664 3 жыл бұрын
sir if we put print ouside of solve function why iam getting solution .can u please reply for this question
@SarahAhmed-on2vy
@SarahAhmed-on2vy 10 ай бұрын
GREAT!!
@williamgarrido5371
@williamgarrido5371 3 жыл бұрын
lo mejor que eh visto .. saludos de colombia
@Pythonenthusiast
@Pythonenthusiast 3 жыл бұрын
Gracias!
@arjunraj5719
@arjunraj5719 2 жыл бұрын
really good sir....thanks
@alinalolik5484
@alinalolik5484 Жыл бұрын
what's name this algorithm?
@teefaloshan3907
@teefaloshan3907 2 жыл бұрын
is it possible to solve it using forward checking?
@mackdine
@mackdine 2 жыл бұрын
thanks, you did great!
@brock2k1
@brock2k1 3 жыл бұрын
Why does the grid in the program not match the grid you show in the example?
@Pythonenthusiast
@Pythonenthusiast 3 жыл бұрын
I had the numbers matching, but after refreshing the website, new sudoku popped and the number changed. I didn't think that changing the numbers to match would add any value as the sudoku solver would work regardless of the numbers :)
@debleenadas1723
@debleenadas1723 Жыл бұрын
I am getting recurssion error. Can anyone help me out here?
@pawishrajhenar1103
@pawishrajhenar1103 7 ай бұрын
me sitting a foundation of ai video workshop and watching this video
@nafi7393
@nafi7393 3 жыл бұрын
Dude!!! Your programme really best *-* It works soo well.. But i want to ask it return all the possible solution for me thats really cool!! But is there any way to stop after getting 2 solutions?? I tried to code that part but that didn't work for me
@dominiktoser8531
@dominiktoser8531 2 жыл бұрын
Me, a batch coder: I have no idea what i'm going to do...
@saaichamp2157
@saaichamp2157 2 жыл бұрын
which python version are u using. mine couldn't recognize numpy as a module
@Pythonenthusiast
@Pythonenthusiast 2 жыл бұрын
Numpy is a separate library, so my guess is that you have not installed it yet.
@sewaanwar3792
@sewaanwar3792 2 жыл бұрын
I did it but the output is this error RecursionError: maximum recursion depth exceeded in comparison Bcs there is two solve() Can someone pls tell me what to do with that error??!🥺
@marioandresgarayrodriguez135
@marioandresgarayrodriguez135 3 жыл бұрын
Bro, does this sudoku code solve only this sudoku matrix or is it suitable for any sudoku?
@Pythonenthusiast
@Pythonenthusiast 3 жыл бұрын
Hey there, it would solve any sudoku (as long as there is a solution).
@birdiehunter5097
@birdiehunter5097 3 жыл бұрын
Nice tutorial! Could you do one for a solution in VBA?
@Pythonenthusiast
@Pythonenthusiast 3 жыл бұрын
Hey there. Unfortunately, I would not be covering a solution in VBA on this channel as I would like to keep it Python-related only.
@arjunraj5719
@arjunraj5719 2 жыл бұрын
also sir..how to code an irregular sudoku.. like a 5x5 sudoku
@Pythonenthusiast
@Pythonenthusiast 2 жыл бұрын
Irregular sudoku, 5x5, so numbers would range from 1 to 25? Solving it can be done with the same code as this one, with some minor tweaks.
@CollosalTrollge
@CollosalTrollge Жыл бұрын
Lets see if i can understand your code, and follow your vdeo, i tried some other channels but i cant manage to grasp it.
@Pythonenthusiast
@Pythonenthusiast Жыл бұрын
I wish you lots of success and I hope this video can help you out! :)
@ujjwalsinghpal8220
@ujjwalsinghpal8220 3 жыл бұрын
Bro i cannot understand how row will change I want to say that if row = 1 then how will row come to 2 then 3 then 4 How number will change How columm will change Pla reply
@Pythonenthusiast
@Pythonenthusiast 3 жыл бұрын
Hey there. In this particular case, a so-called for-loop is being used. Instead of typing row = 1, then row = 2, and so on, we create the for loop "for i in range (0,9)". What this does is, it allocates the numbers 0 up to (not including) 9 to the letter i. Later on, this letter i is what we're using as a row. The same goes for columns, as the same approach is used. Maybe you can rewatch the tutorial, I'm trying to explain that in detail as this is a crucial part of the code.
@ravigupta1813
@ravigupta1813 2 жыл бұрын
Computerphile .?
@nafi7393
@nafi7393 3 жыл бұрын
is it possible to get only one answer? i don't want all other answers
@Pythonenthusiast
@Pythonenthusiast 3 жыл бұрын
Absolutely.
@nafi7393
@nafi7393 3 жыл бұрын
@@Pythonenthusiast can you please tell me where to change the code??
@nafi7393
@nafi7393 3 жыл бұрын
@@Pythonenthusiast hey can you please tell me, how to get only one solution as return even if there are multiple solutions??
@ONE-iv3wi
@ONE-iv3wi 3 жыл бұрын
well explained
@Pythonenthusiast
@Pythonenthusiast 3 жыл бұрын
Thanks a lot!
@HYDEN212
@HYDEN212 3 жыл бұрын
Anymore finance videos?
@Pythonenthusiast
@Pythonenthusiast 3 жыл бұрын
Yes, the next series that I'll work on will be related to Fundamental analysis :)
@4hundreds
@4hundreds 2 жыл бұрын
COPIED FROM PROF. ALTENKIRCH
@paraglide01
@paraglide01 2 жыл бұрын
but a very much better expanation then prof. thorsten altenkirch.
@ramakrishna-wc9sk
@ramakrishna-wc9sk 3 жыл бұрын
For me error coming like import numpy as np ModuleNotFoundError: No module named 'numpy'
@ramakrishna-wc9sk
@ramakrishna-wc9sk 3 жыл бұрын
Please Respond to me
@Pythonenthusiast
@Pythonenthusiast 3 жыл бұрын
@@ramakrishna-wc9sk You need to use pip install numpy. Everything that we import needs to be previously installed. If you are not sure how to install Numpy, there's plenty of information online.
@ramakrishna-wc9sk
@ramakrishna-wc9sk 3 жыл бұрын
@@Pythonenthusiast Traceback (most recent call last): File "C:\Users ama krishna\AppData\Local\Programs\Python\Python38-32\sudoko.py", line 39, in solve() File "C:\Users ama krishna\AppData\Local\Programs\Python\Python38-32\sudoko.py", line 34, in solve solve() File "C:\Users ama krishna\AppData\Local\Programs\Python\Python38-32\sudoko.py", line 34, in solve solve() File "C:\Users ama krishna\AppData\Local\Programs\Python\Python38-32\sudoko.py", line 34, in solve solve() [Previous line repeated 1019 more times] File "C:\Users ama krishna\AppData\Local\Programs\Python\Python38-32\sudoko.py", line 32, in solve if possible(row,column,number): File "C:\Users ama krishna\AppData\Local\Programs\Python\Python38-32\sudoko.py", line 13, in possible for i in range(0,9): RecursionError: maximum recursion depth exceeded in comparison like this coming i'm not understanding
@ramakrishna-wc9sk
@ramakrishna-wc9sk 3 жыл бұрын
@@Pythonenthusiast please respond to my problem
@Pythonenthusiast
@Pythonenthusiast 3 жыл бұрын
@@ramakrishna-wc9sk In the description, you can find a link to the code that I wrote. Compare it with yours and you'll figure where your problem lies. I am sorry, but I cannot spend time solving typos for everyone individually.
Create a Sudoku Solver In Java In 20 Minutes - Full Tutorial
20:25
Coding with John
Рет қаралды 320 М.
Python Sudoku Solver - Computerphile
10:53
Computerphile
Рет қаралды 1,2 МЛН
❌Разве такое возможно? #story
01:00
Кэри Найс
Рет қаралды 6 МЛН
طردت النملة من المنزل😡 ماذا فعل؟🥲
00:25
Cool Tool SHORTS Arabic
Рет қаралды 32 МЛН
Dad gives best memory keeper
01:00
Justin Flom
Рет қаралды 20 МЛН
Future Calls in Serverpod (TSFS)
37:01
Tyler Codes
Рет қаралды 9
Sudoku Solver in Python (using 10 different solving strategies)
18:22
Games Computers Play
Рет қаралды 37 М.
3 PYTHON AUTOMATION PROJECTS FOR BEGINNERS
17:00
Internet Made Coder
Рет қаралды 1,6 МЛН
Python Sudoku Solver Tutorial with Backtracking p.1
17:31
Tech With Tim
Рет қаралды 282 М.
Sudoku Solver in Python
19:22
NeuralNine
Рет қаралды 40 М.
Modern Graphical User Interfaces in Python
11:12
NeuralNine
Рет қаралды 1,5 МЛН
The Masterpiece
1:37:42
Cracking The Cryptic
Рет қаралды 430 М.
Python Resume Projects - You Can Finish in a Weekend
10:38
Tech With Tim
Рет қаралды 1,5 МЛН
❌Разве такое возможно? #story
01:00
Кэри Найс
Рет қаралды 6 МЛН