Тёмный

Create Sudoku Solver with Python in 20 minutes 

Python enthusiast
Подписаться 5 тыс.
Просмотров 66 тыс.
50% 1

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...

Опубликовано:

 

12 сен 2024

Поделиться:

Ссылка:

Скачать:

Готовим ссылку...

Добавить в:

Мой плейлист
Посмотреть позже
Комментарии : 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 ❤
@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!
@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!
@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.
@paytakbaba9424
@paytakbaba9424 Год назад
bro thanks for helping for my project. apart all of that you helped me to understand
@Pythonenthusiast
@Pythonenthusiast Год назад
Glad to hear that!
@parkuuu
@parkuuu Год назад
Recursion is so damn scary and brilliant at the same time. I can only hope to master this thinking style.
@saransp3588
@saransp3588 Год назад
Very good explanation ! Thank you. How we can give 'number' as user input and it will check with conditions ?
@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).
@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
@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
@clouddrive6833
@clouddrive6833 Год назад
it was clear cut explanation ,keep posting videos
@amulyaratna638
@amulyaratna638 Год назад
Thanks bro for such clear explanation
@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
@hobbyistmovement
@hobbyistmovement Год назад
Thanks a ton for this video.
@pranavkochhar9352
@pranavkochhar9352 3 года назад
very well explained. Thank you so much
@Pythonenthusiast
@Pythonenthusiast 3 года назад
Thanks for the kind words!
@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.
@SarahAhmed-on2vy
@SarahAhmed-on2vy 10 месяцев назад
GREAT!!
@melbbb5673
@melbbb5673 2 года назад
I like your explanation!
@ulrichnana9821
@ulrichnana9821 Год назад
best explanation ever thanks you
@Pythonenthusiast
@Pythonenthusiast Год назад
Glad you liked it!
@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
@mackdine
@mackdine 2 года назад
thanks, you did great!
@Danitux11
@Danitux11 Год назад
i copied the exact code from github and it has been 2 minutes running and still didn't get the solution
@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
@ashishzarekar9599
@ashishzarekar9599 11 месяцев назад
For me this code prints original(unsolved) grid only, Could someone help me please?
@williamgarrido5371
@williamgarrido5371 3 года назад
lo mejor que eh visto .. saludos de colombia
@Pythonenthusiast
@Pythonenthusiast 3 года назад
Gracias!
@rowdy7664
@rowdy7664 3 года назад
sir if we put print ouside of solve function why iam getting solution .can u please reply for this question
@arjunraj5719
@arjunraj5719 2 года назад
really good sir....thanks
@teefaloshan3907
@teefaloshan3907 2 года назад
is it possible to solve it using forward checking?
@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
@alinalolik5484
@alinalolik5484 Год назад
what's name this algorithm?
@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).
@Lapusso650
@Lapusso650 10 месяцев назад
*does it appear Not “is it appearing”.
@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.
@debleenadas1723
@debleenadas1723 Год назад
I am getting recurssion error. Can anyone help me out here?
@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
@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 :)
@pawishrajhenar1103
@pawishrajhenar1103 7 месяцев назад
me sitting a foundation of ai video workshop and watching this video
@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??!🥺
@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!
@dominiktoser8531
@dominiktoser8531 2 года назад
Me, a batch coder: I have no idea what i'm going to do...
@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.
@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.
@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.
@HYDEN212
@HYDEN212 3 года назад
Anymore finance videos?
@Pythonenthusiast
@Pythonenthusiast 3 года назад
Yes, the next series that I'll work on will be related to Fundamental analysis :)
@ravigupta1813
@ravigupta1813 2 года назад
Computerphile .?
@ONE-iv3wi
@ONE-iv3wi 3 года назад
well explained
@Pythonenthusiast
@Pythonenthusiast 3 года назад
Thanks a lot!
@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??
@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! :)
@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.
Далее
Python Sudoku Solver - Computerphile
10:53
Просмотров 1,2 млн
НЕВОЗМОЖНЫЙ ЭКСПЕРИМЕНТ
00:39
Просмотров 74 тыс.
Новый уровень твоей сосиски
00:33
А на каком языке ты ДУМАЕШЬ?
00:57
Python Sudoku Solver Tutorial with Backtracking p.1
17:31
3 PYTHON AUTOMATION PROJECTS FOR BEGINNERS
17:00
Просмотров 1,6 млн
Sudoku Solver in Python
19:22
Просмотров 41 тыс.
Python Resume Projects - You Can Finish in a Weekend
10:38
Cursor Is Beating VS Code (...by forking it)
18:00
Просмотров 93 тыс.
Python Sudoku Solver Tutorial with Backtracking p.2
17:43
НЕВОЗМОЖНЫЙ ЭКСПЕРИМЕНТ
00:39
Просмотров 74 тыс.