python - Python3 sudoku return problems -
with code below can check rows , columns see if sudoku programme set correctly, however; function returns 2 answers checkit (rows , cols). there way set code check both rows , cols , homecoming "this sudoku solution correct!" if both happen correct(true) along beingness able homecoming "error in column/row (.append(number))" if either row/col checkit function solution happened incorrect(false)?
import sys scanner import * def createlist(size): if size == 0: homecoming [] else: homecoming [0] + createlist(size -1) def printgrid(gridlist): row in gridlist: print (str(row)+"\n") def rows(g): homecoming len(g) def cols(g): homecoming len(g[0]) def printmatrix(g): in range(0,rows(g),1): 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): rows in g: numbersinrow = [] number in rows: if number != 0 , number in numbersinrow: homecoming g.index(rows),false else: numbersinrow.append(number) homecoming "this sudoku solution correct!" def checkcols(g): cols in g: numbersincol = [] number in cols: if number != 0 , number in numbersincol: homecoming g.index(cols),false else: numbersincol.append(number) homecoming true def checkit(g): checkrows(g) rowsuccess = checkrows(g) print(rowsuccess) checkcols(g) colsuccess = checkcols(g) print(colsuccess) def main(): grid = createlist(9) in range(9): grid[i] = createlist(9) readinput(sys.argv[1],grid) printmatrix(grid) checkit(grid) main() def checkit(g): rows = checkrows(g) cols = checkcols(g) if rows == "good" , cols == "good": print(rowsuccess, colsuccess) elif rows=="good": print("error in col: " + cols) elif cols=="good": print("error in row: " + rows) else: # both rows , cols have error print("error in row: " + rows) print("\nerror in col: " + cols)
edit: regards comment below: seek returning list of error positions if error(s) come (just append error list when comes up). @ end, if list of size 0 (no errors) homecoming true
simply have checkrows(g) , checkcols(g) homecoming "good" if fine , number of row/column error if error comes up.
python python-3.x sudoku
No comments:
Post a Comment