With the code below I can check rows and columns to see if the Sudoku program is set up correctly, however; the function returns two answers for checkIt (rows and cols). Is there a way to set up the code to check both rows and cols and then return with "This Sudoku solution is correct!" if both happen to be correct(True) along with being able to return with "Error in column/row (.append(number))" if either row/col checkIt function solution happened to be incorrect(False)?
import sys
from scanner import *
def createList(size):
if size == 0: return []
else:
return [0] + createList(size -1)
def printGrid(gridlist):
for row in gridlist:
print (str(row)+"\n")
def rows(g):
return len(g)
def cols(g):
return len(g[0])
def printMatrix(g):
for i in range(0,rows(g),1):
for j in range(0,cols(g),1):
print(g[i][j],end=' ')
print('')
print('')
def readinput(filename,grid):
s = Scanner(filename)
r = s.readtoken()
while r != "":
r = int(r)
c = s.readint()
v = s.readint()
grid[r][c]=v
r = s.readtoken()
def checkRows(g):
for rows in g:
numbersInRow = []
for number in rows:
if number != 0 and number in numbersInRow:
return g.index(rows),False
else:
numbersInRow.append(number)
return "This Sudoku solution is correct!"
def checkCols(g):
for cols in g:
numbersInCol = []
for number in cols:
if number != 0 and number in numbersInCol:
return g.index(cols),False
else:
numbersInCol.append(number)
return True
def checkIt(g):
checkRows(g)
rowSuccess = checkRows(g)
print(rowSuccess)
checkCols(g)
colSuccess = checkCols(g)
print(colSuccess)
def main():
grid = createList(9)
for i in range(9):
grid[i] = createList(9)
readinput(sys.argv[1],grid)
printMatrix(grid)
checkIt(grid)
main()