Sudoku Validator using Python

In this article, we have built a program to find whether the given Sudoku Solution is valid or not, with the Python code.(Sudoku Validator using Python). Please install python 3.8 from the link given below to run this code.
https://www.python.org/downloads/

I prefer you to use atom text editor to type your code or to save the given code:
https://atom.io/

You can download the the Sudoku.py from GitHub using the given link:

Link for the code in GitHub

Example Input:

123456789
234567891
345678912
456789123
567891234
678912345
789123456
891234567
912345678

Output:

False

Code:

try:a = input(),input(),input(),input(),input(),input(),input(),input(),input()
except:print('You must enter a 9x9 input')
def vert(a,num):
	x=[]
	for i in range(9):
		x+=(a[i][num])
	return ''.join(x)
b=[]
for i in range(9): b+=list(vert(a,i))+[' ']
b = ''.join(b).split()

box1,box2,box3,box4,box5,box6,box7,box8,box9=[],[],[],[],[],[],[],[],[]
for i in range(3):box1+=(a[i][:3])
box1=''.join(box1)

for i in range(3,6):box2+=(a[i][:3])
box2=''.join(box2)

for i in range(6,9):box3+=(a[i][:3])
box3=''.join(box3)

for i in range(3):box4+=(a[i][3:6])
box4=''.join(box4)

for i in range(3,6):box5+=(a[i][3:6])
box5=''.join(box5)

for i in range(6,9):box6+=(a[i][3:6])
box6=''.join(box6)

for i in range(3):box7+=(a[i][6:])
box7=''.join(box7)

for i in range(3,6):box8+=(a[i][6:])
box8=''.join(box8)

for i in range(6,9):box9+=(a[i][6:])
box9=''.join(box9)


hc,vc,bc = 0,0,0
def horcheck(a):
    hc= 0
    for i in a:
        for j in i:
            if i.count(j)>1:
                hc = 1
                return False
                break
        if hc==1:break
    if hc==0:return True
def vercheck(a):
    vc= 0
    for i in a:
        for j in i:
            if i.count(j)>1:
                vc = 1
                return False
                break
        if vc==1:break
    if vc==0:return True
def boxcheck(a,b,c,d,e,f,g,h,j):
    bc =0
	for i in a:
		if a.count(i)>1:
           bc =1
		   return False
    if  bc ==0:
		for i in b:
			if b.count(i)>1:
	           bc =1
			   return False
    if  bc ==0:
		for i in c:
			if c.count(i)>1:
	           bc =1
			   return False
	if  bc ==0:
		for i in d:
			if d.count(i)>1:
	           bc =1
			   return False
    if  bc ==0:
		for i in e:
			if e.count(i)>1:
	           bc =1
			   return False
	if  bc ==0:
		for i in f:
			if f.count(i)>1:
	           bc =1
			   return False
    if  bc ==0:
		for i in g:
			if g.count(i)>1:
	           bc =1
			   return False
	if  bc ==0:
		for i in h:
			if h.count(i)>1:
	           bc =1
			   return False
    if  bc ==0:
		for i in j:
			if j.count(i)>1:
	           c =1
			   return False
if horcheck(a) and vercheck(b) and boxcheck(box1,box2,box3,box4,box5,box6,box7,box8,box9):
    print(True)
else:
    print(False)

Code Explanation:

In this code, we get 9 inputs from the user, where each input has 9 characters.

At first we Try to get the inputs from the user, and if the user does not enter exactly 9 inputs, the Except statement prints out ‘You must enter a 9×9 input’.

Function vert():

Now in order to check whether the solution characters are vertically unique, we define a function called vert() that takes two parameters.

We store the vertical version of the input in the variable b.

Boxes:

We define variables that store the values of the bigger boxes that is usually seen 3×3 in the game. And since we have 9 big boxes, we define 9 variables (box1, box2, box3, box4, box5, box6, box7, box8, box9)

Sudoku Validator Python
Sudoku

Function horcheck(), vercheck() and boxcheck():

We define another 3 functions horcheck(), vercheck and boxcheck() that checks the character uniqueness horizontally, vertically and with each box respectively.

The functions return True if all the characters are unique horizontally, vertically and with respect to each box, and False is not.

And now, our code works perfectly fine.

Conclusion:

The above article explains the Sudoku Validator using python coding. If you want to download the code from GitHub, click here. If you have any questions or feedback on this, feel free to post in the comments section below.

Comments

No comments yet. Why don’t you start the discussion?

Leave a Reply

Your email address will not be published. Required fields are marked *