11/13/2023 0 Comments Solving sudoku python![]() ![]() We choose a cell with several possibilities (for example one of the cells with the least number of different possibilities). What do we do then? Well, we try the different possibilities. if it returns a grid where there are 0’s left, this means that there are cells with several possibilities. If the simpleSolve does not solve the grid, i.e. The solveGrid function will start by running simpleSolveon the grid, which will also have the added benefit of making a deep copy of the grid. The simpleSolve function The complete solving with solveGrid ![]() (If there are no more 0’s, the grid has been solved.) The function, therefore, returns what it has succeeded in doing with the grid, as well as the number of 0’s remaining. Or, there has been no (or no more) replacement or there are no 0s left in the grid, in which case the function has reached the end of what it can do. Either we have found new values but there are still 0s in the grid, in which case we start a new loop by resetting the counter of 0s. Once the function has looped over all the cells, one of two things can happen. Third, there are several possibilities: in this case, the function increments a counter which is used to measure the number of 0s left in the grid. Second, there is no possibility: in this case, the function returns an error. First, there is only one possibility in a cell: in this case, the function will fill this possibility in the cell (and that’s why we made a deep copy at the beginning). For each cell of the grid that contains a 0, it calls getPossibilities to get the possibilities on this cell. Then the function iterates over all the cells. This basic resolution starts by making a deep copy of the array so as not to alter the array received as a parameter. The function simpleSolve will do a basic resolution as its name indicates. The getPossibilities function A first simple resolution with simpleSolve This function will then return an array that can have no elements (if there is an error in the grid), only one element (which is, therefore, the right element for the square), or several elements. We then iterate from 1 to 9 to find the set of numbers that are not contained in the table of existing numbers, and that are therefore possible candidates for this case. If the cell is currently 0, we look at all the numbers (greater than 0) that are in the same row, column, or 3x3 cell, and we list them in an array. If the cell is not null, the only possibility is the number that is already filled in, so we return an array containing only that number. This function will return the array of all the possibilities, in the current state of the grid, for one of the cells. In this array of numbers 0, 9 we will make a first function called getPossibilities. The retrieval of the possibilities of a cell with getPossibilities In each of these little tables, the nine, well they have nine digits and these nine digits can be between 0 and 9, 0 meaning an empty cell, the digits 1 to 9 meaning the cells already filled in. How is the grid structured? First, we read the existing data and then we create an array, actually an array of arrays. The code is relatively simple, using four functions: The first one reads the data and creates the grid, the second one looks for the possibilities of a square, the third one that solves, that does the first resolution the simple resolution of a grid, and the fourth one that does the complete resolution of the grid. The nice thing about all this is that if you know how to solve sudoku in python, you also know how to generate it. And that’s why I coded this program that does this test. My daughter came to me and said: "Daddy, daddy! Actually, in this grid, there are two possibilities!” And so I wanted to check that I hadn’t made a mistake and that my thing worked. What is the problem I am trying to solve? This is the reason I’m actually doing this: because I started creating a Sudoku book (on KDP) using grids generated by an online service, and PDF generation via React. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |