Solve Sudoku Puzzles Using Python and Recursive Programming!

preview_player
Показать описание
One of the most important concepts in algorithmic learning and data processing in python code is recursion and writing programs that utilize backtracking to process all possible answers!

This video shows start to finish how to take a matrix of the values given at the start of a sudoku, and process that data through two functions checking every space for validity against every possible number!

In the sub function 'valid' we check every row, column and 3x3 grid of blocks against the numbers 1-9 to see whether or not a certain number works in that space, and then in the 'iterate' function utilizes recursion by checking each space only until a valid number is found then goes to the next space and until it finds a space where no valid solution can be found then it begins backtracking until no additional solution is possible.

If you have any questions on what you saw in this video or want to see something specific in the future just let me know about it in the comments below!

Be sure to like and subscribe and check out the rest of the channel for over a hundred more great tutorials!

Thanks for watching and Good luck with your code!
Рекомендации по теме
Комментарии
Автор

So in term of implementation, I found the best way to implement a solver was actually to do this. Check if it's solved, if so return the board, else find the first blank square, if no blank squares return null, else look at the column the empty square is in, look at the row it's in, look at the sub square it's in, all the numbers in those arrays aren't possible values for the empty square, (implementation can be done with sets). Now, if there are no possible values return null, else loop through recursively placing a possible value in the empty square and calling your solve function on that new board, if that board is a solution, return it, else keep looping, if you finish looping through possible values without a solution return null. I implemented this in python, node and c. The c version solved a soduku in under 150 Milliseconds

guillermoparks
Автор

Thank you for this video. I am new to python and just trying to get my head around the backtracking portion of your code. I have played and eventually solved many Sudoku puzzles and can see where a number inserted early in the game can be the wrong number later in the game. So how does the code go all the way back to the first wrong guess and then start over? Is there a pause or sleep function where each iteration is shown on the puzzle?

goutvols
Автор

Hey, great video! I'm a total beginner and thought this was really well explained.

Is this script uploaded to Github? If so could you please share the link?

turk-money
Автор

One thing I don't understand when running my similar code in pycharm debug; When the loop at line ‘for num in range(1, 10):’ sees the count go past 9, it hits the ‘return’ line nearest the bottom of the page, and then returns to the line above it (puzzzle[i][j] = 0). Why there?

abdabzeebop
Автор

i really dont like the usage of global here. if you passed the board in as an argument you wouldn't need to reset the value inside of your iterate function, which is honestly the most annoying and unintuitive part of the code

reedsut