{{ message }}

Instantly share code, notes, and snippets.

# harendra21/Sudoku Solver.py

Created May 9, 2022
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
 def printsudoku(sudoku): print("\n\n") for i in range(len(sudoku)): line = "" if i == 3 or i == 6: print("---------------------") for j in range(len(sudoku[i])): if j == 3 or j == 6: line += "| " line += str(sudoku[i][j])+" " print(line) print("\n\n") def findNextCellToFill(sudoku): for x in range(9): for y in range(9): if sudoku[x][y] == 0: return x, y return -1, -1 def isValid(sudoku, i, j, e): rowOk = all([e != sudoku[i][x] for x in range(9)]) if rowOk: columnOk = all([e != sudoku[x][j] for x in range(9)]) if columnOk: secTopX, secTopY = 3*(i//3), 3*(j//3) for x in range(secTopX, secTopX+3): for y in range(secTopY, secTopY+3): if sudoku[x][y] == e: return False return True return False def solveSudoku(sudoku, i=0, j=0): i, j = findNextCellToFill(sudoku) if i == -1: return True for e in range(1, 10): if isValid(sudoku, i, j, e): sudoku[i][j] = e if solveSudoku(sudoku, i, j): return True sudoku[i][j] = 0 return False