Click here to Skip to main content
15,888,351 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have been trying to write a code to solve sudoku. This method basically involves finding the empty elements in the list of input, then filling the empty elements with the possible values that can be used in that box by deleting the values present in row, column or a box. Then I have tried using the method of backtracking to solve using each and every value present in the list.

Moreover I believe that the remove function is not working as it should have because if it had worked correctly then the value 8 should not have been there in the list in a[1][2].

Python
def print_grid(arr):
	for i in range(9):
		for j in range(9):
			print arr[i][j],
		print ('\n')

def emptyLoc(a):
	l=[0,0]
	for i in range(9):
		for j in range(9):
			if(a[i][j]==0):
				a[i][j] = [1,2,3,4,5,6,7,8,9]
				l[0] = i
				l[1] = j
	return l

def remove(a):
    for i in range(9):
		for j in range(9):
		        if type(a[i][j]) == list :
               	       		x = a[i][j]
                		for k in x:
                    			for l in range(9):
                        			if(k == a[i][l]):
                            				x.remove(k)

    for i in range(9):
		for j in range(9):
                        if type(a[i][j]) == list:
                        	x = a[i][j]
                		for k in x:
                    			for l in range(9):
                        			if(k == a[l][j]):
                            				x.remove(k)

    for i in range(9):
		for j in range(9):
                	if type(a[i][j]) == list:
                		x = a[i][j]
                		for k in x:
                    			for l in range(3):
                        			for m in range(3):
                            				if(k == a[l+i-(i%3)][m+j-(j%3)]):
                            					x.remove(k)

    return a

def solver(arr):
    newArr = emptyLoc(arr)
    finalArr = remove(arr)

    #Changing One element lists to that single element
    for i in range(9):
		for j in range(9):
                	if type(arr[i][j]) == list and len(arr[i][j]) == 1:
                        	arr[i][j] = arr[i][j][0]

    for i in range(9):
		for j in range(9):
                	if not(type(arr[i][j]) == list):
                		return True
            		else :
                		for num in arr[i][j]:
                    			store = arr[i][j]
                    			arr[i][j] = num
                    			if (solver(arr) == True):
                        			return True
                    			else :
                        			arr[i][j] = store
                		return False




if __name__ == "__main__":
    grid=[[3,0,6,5,0,8,4,0,0],
		[5,2,0,0,0,0,0,0,0],
		[0,8,7,0,0,0,0,3,1],
		[0,0,3,0,1,0,0,8,0],
		[9,0,0,8,6,3,0,0,5],
		[0,5,0,0,9,0,6,0,0],
		[1,3,0,0,0,0,2,5,0],
		[0,0,0,0,0,0,0,7,4],
		[0,0,5,2,0,6,3,0,0]]


    if(solver(grid)):
		print_grid(grid)
    else:
		print "No solution exists"


What I have tried:

I dont understand where I am basically going wrong with the code as I recieve an un-backtracked answer. This is the expected output

3 1 6 5 7 8 4 9 2
5 2 9 1 3 4 7 6 8
4 8 7 6 2 9 5 3 1
2 6 3 4 1 5 9 8 7
9 7 4 8 6 3 1 2 5
8 5 1 7 9 2 6 4 3
1 3 8 9 4 7 2 5 6
6 9 2 3 5 1 8 7 4
7 4 5 2 8 6 3 1 9

but I am getting this as an output

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

Somebody please help me with this. Thank You !!!
Posted
Updated 29-Dec-18 6:39am
v3
Comments
Patrice T 29-Dec-18 5:31am    
and the input was ...
Da_keiser 29-Dec-18 9:08am    
Input is mentioned in if __name__ == "__main__" part of the code

1 solution

Your code is a mess, with Python, indentation is not just to help reading the code like with other languages, indentation is the structure of your program.
You can't have approximate indentation.
Just looking at the output tells you where are some problems
3 [1, 4, 7, 9] 6 5 [2, 4, 7] 8 4 [2, 6, 9] [2, 6, 7, 9]

in first line, 4 is defined in cell 6 but is still a possibility in cell 1 and 4
[4, 8] 8 7 [4, 6, 9] [2, 4, 8] [2, 4, 8, 9] [5, 8, 9] 3 1

in third line, 8 is defined in cell 1 but is still a possibility in cell 0, 4, 5 and 6
This tells you that remove is not working in rows.

as for the solver, I don't understand the logic.

Your code do not behave the way you expect, or you don't understand why !

There is an almost universal solution: Run your code on debugger step by step, inspect variables.
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't know what your code is supposed to do, it don't find bugs, it just help you to by showing you what is going on. When the code don't do what is expected, you are close to a bug.
To see what your code is doing: Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.

Debugger - Wikipedia, the free encyclopedia[^]

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]

27.3. pdb — The Python Debugger — Python 3.6.1 documentation[^]
Debugging in Python | Python Conquers The Universe[^]
pdb – Interactive Debugger - Python Module of the Week[^]

The debugger is here to only show you what your code is doing and your task is to compare with what it should do.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900