#loops through each cell to get next one
def nextCell(grid, i, j):
    for x in range(i, 9):
        for y in range(j, 9):
            if grid[x][y] == 0:
                return x, y
    for x in range(0, 9):
        for y in range(0, 9):
            if grid[x][y] == 0:
                return x, y
    return -1, -1


def isValid(grid, i, j, e):
    rowOk = all([e != grid[i][x] for x in range(9)])
    if rowOk:
        columnOk = all([e != grid[x][j] for x in range(9)])
        if columnOk:
            # finding the top left x,y co-ordinates of the section containing the i,j cell
            secTopX, secTopY = 3 * (i // 3), 3 * (j // 3)
            for x in range(secTopX, secTopX + 3):
                for y in range(secTopY, secTopY + 3):
                    if grid[x][y] == e:
                        return False
            return True
    return False


def solveSudoku(grid,i=0,j=0):
    i, j = nextCell(grid, i, j)
    if i == -1:
        return True
    for e in range(1, 10):
        if isValid(grid, i, j, e):
            grid[i][j] = e
            if solveSudoku(grid, i, j):
                return True
            # Undo the current cell for backtracking
            grid[i][j] = 0
    return False

grid1=[[4,0,6,0,0,0,2,0,9],
       [5,7,0,2,0,6,0,0,0],
       [0,0,1,0,0,5,0,0,8],
       [6,0,3,4,8,1,7,0,0],
       [7,0,0,5,0,0,3,0,0],
       [0,0,5,0,0,0,0,0,0],
       [0,8,9,0,0,0,4,3,0],
       [0,6,0,0,0,0,0,0,1],
       [3,0,4,0,0,0,0,6,7]]
solveSudoku(grid1)
for l in grid1:
    print(l)
1dd3d23d6d1c33596e8272d175e94a653c1543c3
<mask token>


def isValid(grid, i, j, e):
    rowOk = all([(e != grid[i][x]) for x in range(9)])
    if rowOk:
        columnOk = all([(e != grid[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 grid[x][y] == e:
                        return False
            return True
    return False


def solveSudoku(grid, i=0, j=0):
    i, j = nextCell(grid, i, j)
    if i == -1:
        return True
    for e in range(1, 10):
        if isValid(grid, i, j, e):
            grid[i][j] = e
            if solveSudoku(grid, i, j):
                return True
            grid[i][j] = 0
    return False


<mask token>

def nextCell(grid, i, j):
    for x in range(i, 9):
        for y in range(j, 9):
            if grid[x][y] == 0:
                return x, y
    for x in range(0, 9):
        for y in range(0, 9):
            if grid[x][y] == 0:
                return x, y
    return -1, -1


def isValid(grid, i, j, e):
    rowOk = all([(e != grid[i][x]) for x in range(9)])
    if rowOk:
        columnOk = all([(e != grid[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 grid[x][y] == e:
                        return False
            return True
    return False


def solveSudoku(grid, i=0, j=0):
    i, j = nextCell(grid, i, j)
    if i == -1:
        return True
    for e in range(1, 10):
        if isValid(grid, i, j, e):
            grid[i][j] = e
            if solveSudoku(grid, i, j):
                return True
            grid[i][j] = 0
    return False


<mask token>

def nextCell(grid, i, j):
    for x in range(i, 9):
        for y in range(j, 9):
            if grid[x][y] == 0:
                return x, y
    for x in range(0, 9):
        for y in range(0, 9):
            if grid[x][y] == 0:
                return x, y
    return -1, -1


def isValid(grid, i, j, e):
    rowOk = all([(e != grid[i][x]) for x in range(9)])
    if rowOk:
        columnOk = all([(e != grid[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 grid[x][y] == e:
                        return False
            return True
    return False


def solveSudoku(grid, i=0, j=0):
    i, j = nextCell(grid, i, j)
    if i == -1:
        return True
    for e in range(1, 10):
        if isValid(grid, i, j, e):
            grid[i][j] = e
            if solveSudoku(grid, i, j):
                return True
            grid[i][j] = 0
    return False


<mask token>
solveSudoku(grid1)
for l in grid1:
    print(l)

def nextCell(grid, i, j):
    for x in range(i, 9):
        for y in range(j, 9):
            if grid[x][y] == 0:
                return x, y
    for x in range(0, 9):
        for y in range(0, 9):
            if grid[x][y] == 0:
                return x, y
    return -1, -1


def isValid(grid, i, j, e):
    rowOk = all([(e != grid[i][x]) for x in range(9)])
    if rowOk:
        columnOk = all([(e != grid[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 grid[x][y] == e:
                        return False
            return True
    return False


def solveSudoku(grid, i=0, j=0):
    i, j = nextCell(grid, i, j)
    if i == -1:
        return True
    for e in range(1, 10):
        if isValid(grid, i, j, e):
            grid[i][j] = e
            if solveSudoku(grid, i, j):
                return True
            grid[i][j] = 0
    return False


grid1 = [[4, 0, 6, 0, 0, 0, 2, 0, 9], [5, 7, 0, 2, 0, 6, 0, 0, 0], [0, 0, 1,
    0, 0, 5, 0, 0, 8], [6, 0, 3, 4, 8, 1, 7, 0, 0], [7, 0, 0, 5, 0, 0, 3, 0,
    0], [0, 0, 5, 0, 0, 0, 0, 0, 0], [0, 8, 9, 0, 0, 0, 4, 3, 0], [0, 6, 0,
    0, 0, 0, 0, 0, 1], [3, 0, 4, 0, 0, 0, 0, 6, 7]]
solveSudoku(grid1)
for l in grid1:
    print(l)

#loops through each cell to get next one
def nextCell(grid, i, j):
    for x in range(i, 9):
        for y in range(j, 9):
            if grid[x][y] == 0:
                return x, y
    for x in range(0, 9):
        for y in range(0, 9):
            if grid[x][y] == 0:
                return x, y
    return -1, -1


def isValid(grid, i, j, e):
    rowOk = all([e != grid[i][x] for x in range(9)])
    if rowOk:
        columnOk = all([e != grid[x][j] for x in range(9)])
        if columnOk:
            # finding the top left x,y co-ordinates of the section containing the i,j cell
            secTopX, secTopY = 3 * (i // 3), 3 * (j // 3)
            for x in range(secTopX, secTopX + 3):
                for y in range(secTopY, secTopY + 3):
                    if grid[x][y] == e:
                        return False
            return True
    return False


def solveSudoku(grid,i=0,j=0):
    i, j = nextCell(grid, i, j)
    if i == -1:
        return True
    for e in range(1, 10):
        if isValid(grid, i, j, e):
            grid[i][j] = e
            if solveSudoku(grid, i, j):
                return True
            # Undo the current cell for backtracking
            grid[i][j] = 0
    return False

grid1=[[4,0,6,0,0,0,2,0,9],
       [5,7,0,2,0,6,0,0,0],
       [0,0,1,0,0,5,0,0,8],
       [6,0,3,4,8,1,7,0,0],
       [7,0,0,5,0,0,3,0,0],
       [0,0,5,0,0,0,0,0,0],
       [0,8,9,0,0,0,4,3,0],
       [0,6,0,0,0,0,0,0,1],
       [3,0,4,0,0,0,0,6,7]]
solveSudoku(grid1)
for l in grid1:
    print(l)