sudoku solver python

Solutions on MaxInterview for sudoku solver python by the best coders in the world

showing results for - "sudoku solver python"
Mira
04 Aug 2018
1board = [
2    [7,8,0,4,0,0,1,2,0],
3    [6,0,0,0,7,5,0,0,9],
4    [0,0,0,6,0,1,0,7,8],
5    [0,0,7,0,4,0,2,6,0],
6    [0,0,1,0,5,0,9,3,0],
7    [9,0,4,0,6,0,0,0,5],
8    [0,7,0,3,0,0,0,1,2],
9    [1,2,0,0,0,7,4,0,0],
10    [0,4,9,2,0,6,0,0,7]
11]
12
13def solve(bo): # this function has recursion, which its own function within itself. Every layer of function within each function, is an empty space after an empty space.
14  find = find_empty(bo)
15  if not find: return True # If it can't find an empty space, it stops the function. The 'solve' function will only return 'True' when the sudoku has been solved.
16  else: row, col = find # retrieves the row and column of the empty space.
17  
18  for i in range (1,10): # 'i' (refered to as num in the valid function) refers to the numbers that are tested in the previously empty position. 
19    if valid(bo, i, (row, col)):  # if the 'for loop' and 'valid' function find a valid entry, it
20      bo[row][col] = i # ...will be inserted into the board, and...
21      if solve(bo): # ...it will restart the solve function on the recently updated board to find a valid value for the next empty space, but if the solve function has been declared 'True' because an empty space could not be found when find_empty was run at the beginning of the function, ...
22        return True # ...this function will return 'True', causing the the function (itself) that called this line to continue running from the 'if statement' that called this line to return 'True', which causes this line to return 'True'. This will repeat until all of the previous recursive calls of this function have returned 'True'.
23      bo[row][col] = 0 # Python will only run this code if it knows that the next/following recursive call has returned 'False'. If the code is running the last iteration of the 'for loop', the previous recursive call can return to the space that this recursive call is running, because this line declares this recursive call's space as zero (a.k.a., an empty value).  
24  return False # The function will return 'False' if a valid value for a space could not be found (i.e., all the iterations for the space could not be found), causing it to continue running the 'for loop' of the previous space from the second 'if statement'.
25
26
27def valid(bo, num, pos): #this checks if a value entry into a space is valid by checking if that same value is already in the same row, column, or box. It will return 'True' if it has found a valid solution.
28  # Check row. # Because of how the solve function called pos, pos[0] refers to the row and pos[1] refers to the column.
29  for i in range(len(bo[0])): # the 'i' refers to the x-value (or column) of the position.
30    if bo[pos[0]][i] == num and pos[1] != i: # if each element in the row == recently added number as long as it isnt in the position that we just inserted.
31      return False
32
33  # Check column.
34  for i in range(len(bo)):
35    if bo[i][pos[1]] == num and pos[0] != i: 
36      return False
37  
38  # Check Box of 3 by 3
39  box_x = pos[1] // 3
40  box_y = pos[0] // 3
41  for i in range(box_y*3, box_y*3 + 3): # this loops through the 3 by 3 box to check whether we have the same element appearing twice.
42    for j in range(box_x*3, box_x*3 + 3):
43      if bo[i][j] == num and (i,j) != pos:
44        return False
45  return True # a function stops running when it reaches a return statement.
46
47
48def print_board(bo):
49  for i in range(len(bo)): # for loop iterating through each row.  'i' is the row, and 'j' is each value in each row. 
50    if i % 3 == 0 and i != 0: print("- - - - - - - - - - - -")
51    
52    for j in range(len(bo[0])): #iterating through each column of each row.
53      if j % 3 == 0 and j != 0: print(" | ", end="")
54        
55      if j == 8: print(bo[i][j])
56      else: print(bo[i][j], end=" ")
57
58
59def find_empty(bo): #this finds empty spaces.
60  for i in range(len(bo)):
61    for j in range(len(bo[0])):
62      if bo[i][j] == 0:
63        return (i, j) # row, then column of empty spaces.
64  return None
65
66
67# execution
68print_board(board)
69print('\n\t {}\n'.format(solve(board)))
70print_board(board)
queries leading to this page
solving sudoku pythonhwo to solve sudoku pythonpython sudoku boardsudoku solver game in pythonpython3 sudoku solverpyhton sudoku solversoduku solver pythonsudoku solver python codesudoku solver pythonphilepython solves sudokusudoku algorithm pythonpython sudoku solversudoku solver with pythonhow to solve sudoku pythonpython sudoku solver inputsudoku using pythonpython sudoku solver solve functionsuduku pythonsudoku solver pythonphiltsudoku solver tim7x7 sudoku solution pythonsudoku solver python tech with timsudoku puzzle pythonpython solution sudokupython script to solve sudokusudoku solver code pythonsolve sudoku pythonusing python to solve sudokusudoku problem solving program in pythonhow to solve sudoku in pythosudoku solver in pythonhow to make sudoku solver code in pythonpython code to solve sudokudef fillallobvious 28 29 3a sudoku pythonsolver sudoku pythonpython sudoku solver source codesudoku solve pythonsudoku in pythonsudoku solver code in pythonsolving sudoku in pythonauto sudoku solver pythonsudoku solver sudoku com pythonsudoku solver in python giving different answerssudoku python codesudoku solver algorithm pythonsudoku solver algorithm pytpython sudokusudoku program in pythonsudoku tech with timsudoku solver using pythonsudoku calculator pythonsudoku solver python projectsudoku solver api pythonsudoku solver pythonsoduu pythonsudoku solver tim p4make a sudoku solver pythonpython solve sudokusudoku program pythonpython sudoku solver tutorialpython sudoku unsolved puzzlescreating sudoku solver pythonsudoko solver pythonsudoko pythonsudoku pythonsudoku solving algorithm pythontech with tim sudokusuduko solver pythonprogram to solve sudoku in pythonpython fast sudoku solverreal time sudoku solver python sudoku python solvesudoku solver game pythonpython sudoku recursion explanedtech with tim sudoku solverhow to make a sudoku solver in pythonhow to solve sudoku in pythonbacktracking sudoku solver pythonsudoku solver python part 2python sudoku solver progrmsudoku solver program pythonpython sudoku solver from linescode to solve sudoku pythonsudoku solver backtrackinghow to code self solving sudokusudoku api pythonsolve sudoku using pythonsudoku python solversudoku oythonsuduko python codesudoku solver python