So, I'm trying to create a function that will take in a list of lists, each consisting of 9 integers numbered 1 through 9, and return a Boolean if it is a valid solution to for a Sudoku. Right now, I've managed to solve part of the problem with checking for rows and columns, but I am stuck on the implementation of checking the three by three boxes. Here's my code so far.
alist = [1,2,3,4,5,6,7,8,9]
def checkSudoku(grid):
a = grid
b = grid
c = grid
d = False
e = False
f = False
newlist = []
for i in a:
i.sort()
if i == alist:
d = True
else:
d = False
break
for j in range(9):
for k in b:
newlist.append(k)
newlist.sort()
if i == alist:
e = True
newlist = []
else:
e = False
break
if d == True and e == True:
return True
else:
return False
Basically, My was to test all three factors necessary for it to be true, then return True if all three are True, else return false. Any help?
athroughfvariables mean without digging through a bunch of code. If they had meaninful names, I might be able to see issues immediately. Single letter variable names are often bad except in very limited situations (iandjare often traditional index variables in loops, andx,yandzhave long mathematical tradition as coordinate names, etc). - BlckknghtFalse, you could just be returningFalse. You may also be interested in taking a look at thesetclass. - U2EF1