Amer's Corner on the Interwebs

Python Programming

Sudoku Solver

Code

import itertools    # For creating cartesian product for naming boxes

all_boxes={}

ab=['A','B','C','D','E','F','G','H','I']
number=['1','2','3','4','5','6','7','8','9']

a=list(itertools.product(ab,number))
b=[]
for i in a:
    c=''.join(i)
    b.append(c)

for i in b:
    all_boxes[i]=['1','2','3','4','5','6','7','8','9']

del a
#del b
del c

def input_num():     # Function to take input from the user to fill the given boxes
    print("Enter the values")
    for i in b:
        a=input(i)
        if a=='':
            pass
        else:
            all_boxes[i]=list(a)

def column(x):    # To return the column of which 'x' is a part
    for i in ab:
        if x[0]==i:
            return list(i+str(j) for j in number)

def row(x):     # To return the row of which 'x' is a part
    for i in number:
        if x[1]==i:
            return list(str(j)+i for j in ab)

def box(x):     # To return the box of which 'x' is a part
    for i in e:
        for j in i:
            if j==x:
                return i

# For creating list of boxes

abc=['A','B','C']
defg=['D','E','F']
ghi=['G','H','I']

a123=['1','2','3']
a456=['4','5','6']
a789=['7','8','9']

abcd=[abc,defg,ghi]

a1234=[a123,a456,a789]

def gen_box():    # Generate box
    global boxes
    boxes=[]
    for i in abcd:
        for j in a1234:
            box=[]
            for k in i:
                for l in j:
                 box.append(list(itertools.product(k,l)))
            boxes.append(box)
    #return boxes

gen_box()
d=[]
for i in boxes:
    e=[]
    for j in i:
        f=[]
        for k in j:
            g=[]
            for l in k:
                c=''.join(l)
                g.append(c)
            f.append(g)
        e.append(f)
    d.append(e)

e=[]     # Final list with the list of boxes
for i in d:
    f=[]
    for j in i:
        for k in j:
            l=k[0]+k[1]
        f.append(l)
    e.append(f)

def solve_column(box,num):    # To remove the 'num' from the column of which 'box' is a part
    for i in column(box):
        if num in all_boxes[i]:
            if len(all_boxes[i])>1:
                all_boxes[i].remove(num)

def solve_row(box,num):     # To remove the 'num' from the row of which 'box' is a part
    for i in row(box):
        if num in all_boxes[i]:
            if len(all_boxes[i])>1:
                all_boxes[i].remove(num)

def solve_box(box1,num):   # To remove the 'num' from the boxe of which 'box' is a part
    for i in box(box1):
        if num in all_boxes[i]:
            if len(all_boxes[i])>1:
                all_boxes[i].remove(num)

def check_state():      # To check is the Sudoku is solved or not
    for i in all_boxes:
        if len(all_boxes[i])>1:
            return "Not_solved"
    else:
        return "Solved"

def solver():     # The main loop the solves the Sudoku
    while check_state()=="Not_solved":
        for i in all_boxes:
            if len(all_boxes[i])==1:
                solve_column(i,all_boxes[i][0])
                solve_row(i,all_boxes[i][0])
                solve_box(i,all_boxes[i][0])

def num_of_unsolved_boxes():    # To know the number of unsolved boxes
    n=0
    for i in all_boxes:
        if len(all_boxes[i])>1:
            n+=1
    return n

input_num()
solver()