Create Sudoku Solver with Python in 20 minutes

Поделиться
HTML-код
  • Опубликовано: 12 янв 2025

Комментарии • 102

  • @JuliusKhoo
    @JuliusKhoo 3 года назад +37

    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 года назад +5

      Thanks a lot for the kind words, it means a lot to me!

  • @alitoto5452
    @alitoto5452 2 года назад +5

    absolute legend if you are unsure to watch this video or another one then watch this one 100% best explanation ever

  • @josecarrera6519
    @josecarrera6519 3 года назад +2

    this is super intuitive, we are solving a sudoku program at uni and ours is so much more complicated

    • @Pythonenthusiast
      @Pythonenthusiast  3 года назад +2

      I am happy to read a comment such as this one :)

  • @reiherentenorbert7487
    @reiherentenorbert7487 3 года назад +2

    Very elegantly solved! - I once wrote a solver myself, wrote much more code and did only find one solution. Excellent!

    • @Pythonenthusiast
      @Pythonenthusiast  3 года назад +2

      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

  • @M1EEA
    @M1EEA Год назад +1

    That's the most clear explanation i have found ❤

  • @sumtpathak19
    @sumtpathak19 2 года назад +1

    Bro, you explain this logic clearly thank you so much and keep growing

  • @raveenachakrapani1090
    @raveenachakrapani1090 3 года назад +4

    What a precise and clear explanation !!! Thanks a ton !!

    • @Pythonenthusiast
      @Pythonenthusiast  3 года назад

      Thanks for the kind words, great you enjoyed the tutorial!

  • @parkuuu
    @parkuuu Год назад

    Recursion is so damn scary and brilliant at the same time. I can only hope to master this thinking style.

  • @ChessQuickiesEcon
    @ChessQuickiesEcon 3 года назад +1

    Tysm!
    This project was short and easy to understand and clear.
    you just got an extra like!

  • @paytakbaba9424
    @paytakbaba9424 2 года назад +1

    bro thanks for helping for my project. apart all of that you helped me to understand

  • @randyfriend
    @randyfriend 3 года назад +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 3 года назад +1

      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 3 года назад +1

      @@Dylan-zx6ji interesting. I just did some negatives and it does round on them. Good to know.

  • @clouddrive6833
    @clouddrive6833 2 года назад

    it was clear cut explanation ,keep posting videos

  • @amulyaratna638
    @amulyaratna638 Год назад

    Thanks bro for such clear explanation

  • @goutvols103
    @goutvols103 7 месяцев назад

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

  • @ulrichnana9821
    @ulrichnana9821 2 года назад

    best explanation ever thanks you

  • @saransp3588
    @saransp3588 2 года назад +2

    Very good explanation ! Thank you. How we can give 'number' as user input and it will check with conditions ?

  • @pranavkochhar9352
    @pranavkochhar9352 3 года назад +1

    very well explained. Thank you so much

  • @dominiktoser8531
    @dominiktoser8531 2 года назад

    Me, a batch coder: I have no idea what i'm going to do...

  • @melbbb5673
    @melbbb5673 3 года назад

    I like your explanation!

  • @gmax30
    @gmax30 2 года назад +3

    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 года назад +1

      The return statement at the bottom of the loop "range(1,10)" makes it go back

  • @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 года назад +1

      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.

  • @carlocirillo8708
    @carlocirillo8708 2 года назад +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 года назад +1

      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

  • @superprocz
    @superprocz 2 года назад +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 года назад +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

  • @markob2571
    @markob2571 Год назад +2

    What is numpy?

  • @JoseMBrito-iu6hg
    @JoseMBrito-iu6hg 2 года назад +1

    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 года назад +4

      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.

  • @arjunraj5719
    @arjunraj5719 3 года назад

    really good sir....thanks

  • @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

  • @williamgarrido5371
    @williamgarrido5371 3 года назад

    lo mejor que eh visto .. saludos de colombia

  • @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

  • @teefaloshan3907
    @teefaloshan3907 2 года назад

    is it possible to solve it using forward checking?

  • @mackdine
    @mackdine 2 года назад

    thanks, you did great!

  • @rowdy7664
    @rowdy7664 3 года назад

    sir if we put print ouside of solve function why iam getting solution .can u please reply for this question

  • @ashishzarekar9599
    @ashishzarekar9599 Год назад

    For me this code prints original(unsolved) grid only, Could someone help me please?

  • @niranjanshankaran3493
    @niranjanshankaran3493 3 года назад +1

    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 3 года назад +1

      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

  • @pawishrajhenar1103
    @pawishrajhenar1103 11 месяцев назад

    me sitting a foundation of ai video workshop and watching this video

  • @saaichamp2157
    @saaichamp2157 3 года назад

    which python version are u using. mine couldn't recognize numpy as a module

    • @Pythonenthusiast
      @Pythonenthusiast  3 года назад

      Numpy is a separate library, so my guess is that you have not installed it yet.

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

  • @marioandresgarayrodriguez135
    @marioandresgarayrodriguez135 3 года назад

    Bro, does this sudoku code solve only this sudoku matrix or is it suitable for any sudoku?

    • @Pythonenthusiast
      @Pythonenthusiast  3 года назад +2

      Hey there, it would solve any sudoku (as long as there is a solution).

  • @Danitux11
    @Danitux11 2 года назад

    i copied the exact code from github and it has been 2 minutes running and still didn't get the solution

  • @Lapusso650
    @Lapusso650 Год назад

    *does it appear
    Not “is it appearing”.

  • @alinalolik5484
    @alinalolik5484 2 года назад

    what's name this algorithm?

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

  • @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??!🥺

  • @debleenadas1723
    @debleenadas1723 Год назад

    I am getting recurssion error. Can anyone help me out here?

  • @CollosalTrollge
    @CollosalTrollge 2 года назад

    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  2 года назад

      I wish you lots of success and I hope this video can help you out! :)

  • @rabihbadr54
    @rabihbadr54 2 года назад

    Explain why you use range(0,9) when you can use range(9)

    • @Pythonenthusiast
      @Pythonenthusiast  2 года назад

      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.

  • @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

  • @arjunraj5719
    @arjunraj5719 3 года назад

    also sir..how to code an irregular sudoku.. like a 5x5 sudoku

    • @Pythonenthusiast
      @Pythonenthusiast  3 года назад

      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.

  • @ahmednasser6757
    @ahmednasser6757 3 года назад

    why it is for `number in range(1, 10)`
    and not for `number in range(1, 9) ` ??

    • @Pythonenthusiast
      @Pythonenthusiast  3 года назад +1

      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!

  • @HYDEN212
    @HYDEN212 4 года назад +1

    Anymore finance videos?

    • @Pythonenthusiast
      @Pythonenthusiast  4 года назад

      Yes, the next series that I'll work on will be related to Fundamental analysis :)

  • @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??

  • @ujjwalsinghpal8220
    @ujjwalsinghpal8220 4 года назад

    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  4 года назад +2

      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 года назад +1

    Computerphile .?

  • @ONE-iv3wi
    @ONE-iv3wi 3 года назад

    well explained

  • @SarahAhmed-on2vy
    @SarahAhmed-on2vy Год назад

    GREAT!!

  • @ramakrishna-wc9sk
    @ramakrishna-wc9sk 4 года назад

    For me error coming like
    import numpy as np
    ModuleNotFoundError: No module named 'numpy'

    • @ramakrishna-wc9sk
      @ramakrishna-wc9sk 4 года назад

      Please Respond to me

    • @Pythonenthusiast
      @Pythonenthusiast  4 года назад

      @@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 4 года назад

      @@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 4 года назад

      @@Pythonenthusiast please respond to my problem

    • @Pythonenthusiast
      @Pythonenthusiast  4 года назад +9

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

  • @4hundreds
    @4hundreds 3 года назад +2

    COPIED FROM PROF. ALTENKIRCH

    • @paraglide01
      @paraglide01 2 года назад

      but a very much better expanation then prof. thorsten altenkirch.