sudoku solver python

board = [
    [7,8,0,4,0,0,1,2,0],
    [6,0,0,0,7,5,0,0,9],
    [0,0,0,6,0,1,0,7,8],
    [0,0,7,0,4,0,2,6,0],
    [0,0,1,0,5,0,9,3,0],
    [9,0,4,0,6,0,0,0,5],
    [0,7,0,3,0,0,0,1,2],
    [1,2,0,0,0,7,4,0,0],
    [0,4,9,2,0,6,0,0,7]
]

def 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.
  find = find_empty(bo)
  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.
  else: row, col = find # retrieves the row and column of the empty space.
  
  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. 
    if valid(bo, i, (row, col)):  # if the 'for loop' and 'valid' function find a valid entry, it
      bo[row][col] = i # ...will be inserted into the board, and...
      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, ...
        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'.
      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).  
  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'.


def 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.
  # Check row. # Because of how the solve function called pos, pos[0] refers to the row and pos[1] refers to the column.
  for i in range(len(bo[0])): # the 'i' refers to the x-value (or column) of the position.
    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.
      return False

  # Check column.
  for i in range(len(bo)):
    if bo[i][pos[1]] == num and pos[0] != i: 
      return False
  
  # Check Box of 3 by 3
  box_x = pos[1] // 3
  box_y = pos[0] // 3
  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.
    for j in range(box_x*3, box_x*3 + 3):
      if bo[i][j] == num and (i,j) != pos:
        return False
  return True # a function stops running when it reaches a return statement.


def print_board(bo):
  for i in range(len(bo)): # for loop iterating through each row.  'i' is the row, and 'j' is each value in each row. 
    if i % 3 == 0 and i != 0: print("- - - - - - - - - - - -")
    
    for j in range(len(bo[0])): #iterating through each column of each row.
      if j % 3 == 0 and j != 0: print(" | ", end="")
        
      if j == 8: print(bo[i][j])
      else: print(bo[i][j], end=" ")


def find_empty(bo): #this finds empty spaces.
  for i in range(len(bo)):
    for j in range(len(bo[0])):
      if bo[i][j] == 0:
        return (i, j) # row, then column of empty spaces.
  return None


# execution
print_board(board)
print('\n\t {}\n'.format(solve(board)))
print_board(board)

Are there any code examples left?
Create a Free Account
Unlock the power of data and AI by diving into Python, ChatGPT, SQL, Power BI, and beyond.
Sign up
Develop soft skills on BrainApps
Complete the IQ Test
Relative searches
code to solve sudoku python sudoku solver python part 2 python fast sudoku solver python3 sudoku solver sudoku solve python sudoku solver with python sudoku solver program python python solves sudoku hwo to solve sudoku python solver sudoku python sudoku solver pythonphile sudoku solver pythonphilt sudoku solving algorithm python sudoku solver in python using python to solve sudoku real time sudoku solver python sudoku solver algorithm pyt python sudoku solver tutorial sudoku solver game in python creating sudoku solver python auto sudoku solver python solve sudoku using python python sudoku solver input sudoku using python python solve sudoku python code to solve sudoku sudoku program in python sudoku tech with tim python sudoku solver solve function suduko python code tech with tim sudoku how to make sudoku solver code in python how to code self solving sudoku soduu python sudoku algorithm python soduku solver python solving sudoku in python sudoku solver python code sudoku solver tim p4 sudoku solver tim sudoku calculator python solve sudoku python backtracking sudoku solver python sudoku program python sudoku oython python script to solve sudoku sudoku python code sudoku solver api python make a sudoku solver python sudoku solver using python sudoku api python def fillAllObvious(): sudoku python python sudoku unsolved puzzles python sudoku board 7x7 sudoku solution python sudoku solver algorithm python how to make a sudoku solver in python python sudoku recursion explaned tech with tim sudoku solver Python solution sudoku python sudoku sudoku solver game python sudoku problem solving program in python solving sudoku python sudoko solver python sudoku solver backtracking sudoku solver code in python sudoku python sudoku in python sudoko python suduku python sudoku puzzle python python sudoku solver how to solve sudoku python sudoku solver python
Made with love
This website uses cookies to make IQCode work for you. By using this site, you agree to our cookie policy

Welcome Back!

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign in
Recover lost password
Or log in with

Create a Free Account

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign up
Or sign up with
By signing up, you agree to the Terms and Conditions and Privacy Policy. You also agree to receive product-related marketing emails from IQCode, which you can unsubscribe from at any time.
Creating a new code example
Code snippet title
Source