<jupyter_start><jupyter_text>Algorithm to solve sudoku<jupyter_code>mylist[0][3]
mylist
board = mylist.copy()
board
# So called hardest sudoku puzzle

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


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

# 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):
    find = find_empty(bo)
    if not find:
        return True
    else:
        row, col = find

    for i in range(1,10):
        if valid(bo, i, (row, col)):
            bo[row][col] = i

            if solve(bo):
                return True

            bo[row][col] = 0

    return False


def valid(bo, num, pos):
    # Check row
    for i in range(len(bo[0])):
        if bo[pos[0]][i] == num and pos[1] != i:
            return False

    # Check column
    for i in range(len(bo)):
        if bo[i][pos[1]] == num and pos[0] != i:
            return False

    # Check box
    box_x = pos[1] // 3
    box_y = pos[0] // 3

    for i in range(box_y*3, box_y*3 + 3):
        for j in range(box_x * 3, box_x*3 + 3):
            if bo[i][j] == num and (i,j) != pos:
                return False

    return True


def print_board(bo):
    for i in range(len(bo)):
        if i % 3 == 0 and i != 0:
            print("- - - - - - - - - - - - - ")

        for j in range(len(bo[0])):
            if j % 3 == 0 and j != 0:
                print(" | ", end="")

            if j == 8:
                print(bo[i][j])
            else:
                print(str(bo[i][j]) + " ", end="")


def find_empty(bo):
    for i in range(len(bo)):
        for j in range(len(bo[0])):
            if bo[i][j] == 0:
                return (i, j)  # row, col

    return None

print_board(board)
solve(board)
print("___________________")
print_board(board)
mylist
mylist = np.array(mylist, dtype="int")
mylist
board = mylist
board
rows, cols = (9, 9) 
arr = [[0]*cols]*rows 
print(arr) 
subtract_set = {1,2,3,4,5,6,7,8,9}

def check_horizontal(i,j,container):
    return subtract_set - set(container[i])

def check_vertical(i, j, container):
    ret_set = []
    for x in range(9):
        ret_set.append(container[x][j])
    return subtract_set - set(ret_set)

def check_square(i, j, container):
    first = [0,1,2]
    second = [3,4,5]
    third = [6,7,8]
    find_square = [first, second, third]
    for l in find_square:
        if i in l:
            row = l
        if j in l:
            col = l
    return_set = []
    for x in row:
        for y in col:
            return_set.append(container[x][y])
    return subtract_set - set(return_set)

def get_poss_vals(i, j, container):
    poss_vals = list(check_square(i, j, container) \
                    .intersection(check_horizontal(i, j, container)) \
                    .intersection(check_vertical(i, j, container)))
    return poss_vals

def explicit_solver(container):
    for i in range(9):
        for j in range(9):
            if container[i][j] == 0:
                poss_vals = get_poss_vals(i, j, container)
                if len(poss_vals) == 1:
                    container[i][j] = list(poss_vals)[0]
                    print_container(container)
    return container
explicit_solver(board)
import numpy as np

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


def possible(x, y, n):
    for i in range(0, 9):
        if grid[i][x] == n and i != y: # Checks for number (n) in X columns
            return False


    for i in range(0, 9):
        if grid[y][i] == n and i != x: # Checks for number (n) in X columns
            return False

    x0 = (x // 3) * 3
    y0 = (y // 3) * 3
    for X in range(x0, x0 + 3):
        for Y in range(y0, y0 + 3):  # Checks for numbers in box(no matter the position, it finds the corner)
            if grid[Y][X] == n:
                return False    
    return True

def solve():
    global grid
    for y in range(9):
        for x in range(9):
            if grid[y][x] == 0:
                for n in range(1, 10):
                    if possible(x, y, n):
                        grid[y][x] = n
                        solve()
                        grid[y][x] = 0
                return
    print(np.matrix(grid))

solve()<jupyter_output>[[6 7 2 8 9 4 3 5 1]
 [8 3 1 5 2 7 4 6 9]
 [5 4 9 1 6 3 2 8 7]
 [1 5 7 3 8 2 9 4 6]
 [3 9 6 4 7 5 1 2 8]
 [2 8 4 9 1 6 5 7 3]
 [9 2 8 6 5 1 7 3 4]
 [7 6 3 2 4 9 8 1 5]
 [4 1 5 7 3 8 6 9 2]]
{'fraction_non_alphanumeric': 0.19053140096618357, 'fraction_numerical': 0.09391304347826086, 'mean_word_length': 1.8803561491374514, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 4, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '337994', 'license': 'MIT', 'n_tokens_mistral': 2407, 'n_tokens_neox': 2271, 'n_words': 525, 'path': 'Untitled.ipynb', 'repo_name': 'rahulkuriyedath/Sudoku', 'size': '5175'}