简体   繁体   English

Python3数独返回问题

[英]Python3 sudoku return problems

With the code below I can check rows and columns to see if the Sudoku program is set up correctly, however; 通过下面的代码,我可以检查行和列,以查看Sudoku程序是否设置正确。 the function returns two answers for checkIt (rows and cols). 该函数为checkIt返回两个答案(行和列)。 Is there a way to set up the code to check both rows and cols and then return with "This Sudoku solution is correct!" 有没有办法设置代码来检查行和列,然后返回“此Sudoku解决方案是正确的!” 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)? 如果两者都正确(True),并且能够返回“列/行错误(.append(number))”,则行/列检查中的任何一个函数解决方案都不正确(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()
def checkIt(g):
   rows = checkRows(g)
   cols = checkCols(g)
   if rows == "good" and cols == "good":
       print(rowSuccess, colSuccess)
   elif rows=="good":
       print("Error in col: " + cols)
   elif cols=="good":
       print("Error in row: " + rows)
   else: # Both rows and cols have an error
       print("Error in row: " + rows)
       print("\nError in col: " + cols)

EDIT: With regards to comment below: Try returning a list of error positions if error(s) come up (just append an error to the list when it comes up). 编辑:关于以下评论:如果出现错误,请尝试返回错误位置列表(出现错误时只需将错误附加到列表中)。 At the end, if the list is of size 0 (no errors) just return True 最后,如果列表的大小为0(无错误),则返回True

Simply have checkRows(g) and checkCols(g) return "good" if everything is fine and the number of the row/column where the error is if an error comes up. 只需让checkRows(g)和checkCols(g)返回“好”(如果一切都很好),如果出现错误,则返回错误所在的行/列的编号。

First make checkRows return a list of conflicts 首先使checkRows返回冲突列表

rowConflicts = checkRows(g)
colConflicts = checkCols(g)

for conflict in rowConflicts:
    print("Error in row: " + conflict)
for conflict in colConflicts:
    print("Error in row: " + conflict)

if len(rowConflicts) = 0 and len(colConflicts) = 0:
    print("Success!")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM