Hi all,
Effectively what I'm trying to do is to write a sudoku solver in python. I wrote one in Matlab a while ago at undergraduate, so I have a basic idea of how to do so. I'm running it on Spyder as part of the latest Anaconda package.
My approach is to create 2 matrices using lists. Here list A is 2 dimensional representing the sudoku, and all unknown numbers are set to 0. List M is 3 dimensional, where the 3rd dimension indicates for which number it would be possible to have the value: a 1 if it is still possible, a 0 if not.
For example if A[2][2] = 4 (in the sudoku, the third row and 3rd column shows a 4), then A[2][1] cannot be 4, so M[2][1][4 - 1] must be 0 (minus one because python lists start at zero).
However A[3][3] can still be 4, so M[3][3][4-1] should still be 1.
To set these values to zero, I've written the following:
What am I doing wrong?
Full code:
Effectively what I'm trying to do is to write a sudoku solver in python. I wrote one in Matlab a while ago at undergraduate, so I have a basic idea of how to do so. I'm running it on Spyder as part of the latest Anaconda package.
My approach is to create 2 matrices using lists. Here list A is 2 dimensional representing the sudoku, and all unknown numbers are set to 0. List M is 3 dimensional, where the 3rd dimension indicates for which number it would be possible to have the value: a 1 if it is still possible, a 0 if not.
For example if A[2][2] = 4 (in the sudoku, the third row and 3rd column shows a 4), then A[2][1] cannot be 4, so M[2][1][4 - 1] must be 0 (minus one because python lists start at zero).
However A[3][3] can still be 4, so M[3][3][4-1] should still be 1.
To set these values to zero, I've written the following:
if A[i][j] > 0 :
"""set this row to no longer being able to
use this number - dummy variable a """
for a in range(9):
M[a][j][ A[i][j] - 1 ] = 0
"""set this column to no longer being able to
use this number - dummy variable a """
for a in range(9):
M[i][a][ A[i][j] - 1 ] = 0However this sets all 4th numbers in the entire M to zero, so both M[2][1][3] and M[3][3][3]. What am I doing wrong?
Full code:
def openfile(csvfile):
"""Open matrix from csv file (comma delimited only)"""
import csv
matrix = []
with open(csvfile, newline = '') as inputfile:
for row in csv.reader(inputfile) :
for i in range(len(row)):
row[i] = int(row[i])
matrix.append(row)
return matrix
""" START PROGRAMME"""
"""import matrix """
startmatrix = openfile('matrixinput.csv')
"""Create full possibilities matrix A & M - dummy variable a"""
M = []
a = []
A = startmatrix
for i in range(9) :
a.append([1, 1, 1, 1, 1, 1, 1, 1, 1])
for i in range(9) :
M.append(a)
""" run sodoku solver """
""" Function to solve Sudokus without guessing, adding something in to
keep it running until finished
A is 2D matrix solution
M is 3D matrix of possibilities (1s & 0s)
"""
flag = 0
AA = []
MM = []
while flag == 0:
""" Update matrix
Stop cycling if neither A nor M changed """
AA = A
MM = M
for i in range(9) :
for j in range(9) :
if A[i][j] > 0 :
"""set this row to no longer being able to
use this number - dummy variable a """
for a in range(9):
M[a][j][ A[i][j] - 1 ] = 0
"""set this column to no longer being able to
use this number - dummy variable a """
for a in range(9):
M[i][a][ A[i][j] - 1 ] = 0
"""set this quadrant to 0 for this value, dummmy variables:
a - which quadrant row
b - which quadrant column
l , k - dummy variables
"""
a = round( i / 3 - 0.48 , 0)
b = round( j / 3 - 0.48 , 0)
for l in range(3) :
for k in range(3):
M[3*a + l][3*b + k][A[i][j]-1] = 0
"""Clear all other possibilities from A[i][j], dummy = a
but ensure the correct value is a possibility
"""
for a in range(9) :
M[i][j][a] = 0
M[i][j][A[i][j]-1] = 1
""" If there is only one possibility left, update A
"""
else :
if sum(M[i][j][:]) == 1 :
a = M[i][j][:].index(1) + 1
A[i][j] = a
if M == MM and A == AA :
flag = 1
print(A)
print(M)
