Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

sudoku solver py

M = 9
def puzzle(a):
    for i in range(M):
        for j in range(M):
            print(a[i][j],end = " ")
        print()
def solve(grid, row, col, num):
    for x in range(9):
        if grid[row][x] == num:
            return False
             
    for x in range(9):
        if grid[x][col] == num:
            return False
 
 
    startRow = row - row % 3
    startCol = col - col % 3
    for i in range(3):
        for j in range(3):
            if grid[i + startRow][j + startCol] == num:
                return False
    return True
 
def Suduko(grid, row, col):
 
    if (row == M - 1 and col == M):
        return True
    if col == M:
        row += 1
        col = 0
    if grid[row][col] > 0:
        return Suduko(grid, row, col + 1)
    for num in range(1, M + 1, 1): 
     
        if solve(grid, row, col, num):
         
            grid[row][col] = num
            if Suduko(grid, row, col + 1):
                return True
        grid[row][col] = 0
    return False
 
'''0 means the cells where no value is assigned'''
grid = [[2, 5, 0, 0, 3, 0, 9, 0, 1],
        [0, 1, 0, 0, 0, 4, 0, 0, 0],
    [4, 0, 7, 0, 0, 0, 2, 0, 8],
    [0, 0, 5, 2, 0, 0, 0, 0, 0],
    [0, 0, 0, 0, 9, 8, 1, 0, 0],
    [0, 4, 0, 0, 0, 3, 0, 0, 0],
    [0, 0, 0, 3, 6, 0, 0, 7, 2],
    [0, 7, 0, 0, 0, 0, 0, 0, 3],
    [9, 0, 3, 0, 0, 0, 6, 0, 4]]
 
if (Suduko(grid, 0, 0)):
    puzzle(grid)
else:
    print("Solution does not exist:(")
Comment

PREVIOUS NEXT
Code Example
Python :: import login required 
Python :: scaling pkl file? 
Python :: flask get uploaded file size 
Python :: use a csv file on internet as an api in python 
Python :: concact geodataframe python 
Python :: zip a directory in python 
Python :: run python script automatically every day 
Python :: python convert ascii to char 
Python :: Genisim python 
Python :: reading the JSON from a JSON object 
Python :: sns histplot nan values 
Python :: dataframe number of unique rows 
Python :: axis labels python 
Python :: initialize np array 
Python :: python generator expression 
Python :: percent in pandas 
Python :: python sqrt 
Python :: how to kill somene 
Python :: Box Plot, Python 
Python :: copy dataframe columns names 
Python :: dictionary in python 
Python :: seaborn boxplot legend color 
Python :: read a csv file in pandas 
Python :: python tuple and dictionary 
Python :: identity matrix python 
Python :: Converting objects into integers in python 
Python :: pygame bg color 
Python :: python -c 
Python :: sort a dictionary by value then key 
Python :: py function 
ADD CONTENT
Topic
Content
Source link
Name
7+3 =