基本数独

2024-04-19 06:59:48 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在用Python创建一个基本的数独检查器。 但是,结果始终显示为“False”。 代码如下:

list = [[1,2,3,4],
        [2,4,1,3],
        [3,1,4,2],
        [4,3,2,1]]

def sudoku_check(list):
    n = len(list)
    digit = 1
    while digit <= n:
        row_count = 0
        col_count = 0
        v = 0
        while v < n:
            x = 0
            while x < n:
                print ("Check")
                if digit == list[v][x]:
                    row_count = row_count + 1
                if digit == list[x][v]:
                    col_count = col_count + 1
                x = x + 1
            v = v + 1
        if col_count != 1 or row_count != 1:
            return False
        digit = digit + 1
    return True

print (sudoku_check(list))

我是编程新手。非常感谢你的帮助。 谢谢


Tags: 代码falselenreturnifdefcheckcount
1条回答
网友
1楼 · 发布于 2024-04-19 06:59:48

好吧,给你一个解决方案/可以解释你的问题@shryashkarnik!你知道吗

问题是:

代码中的问题来自以下区块:

while digit <= n:
    row_count = 0
    col_count = 0
    v = 0
    while v < n:
        x = 0
        while x < n:
            print ("Check")
            if digit == sudo_board[v][x]:
                row_count = row_count + 1
            if digit == sudo_board[x][v]:
                col_count = col_count + 1
            x = x + 1
        v = v + 1
    if col_count != 1 or row_count != 1:
        return False

那么这个代码到底在做什么呢?它会穿过你数独板上的每个单元格,寻找一个数字。为了解释,假设它在寻找数字1。它检查整个电路板中的每一个单元,由于1总共出现了4次,所以col_countrow_count每次都是4。如果你愿意的话,你可以用打印语句来验证!你知道吗

由于您的错误检查是针对数字1进行检查,因此每次都会失败。所以让我们开始寻找解决办法!你知道吗

让事情变得像Python

"Pythonic means code that doesn't just get the syntax right but that follows the conventions of the Python community"。你说你是编程新手,所以学习如何编写python的正确风格是很重要的。上面的代码中有几个问题:

  • 混淆变量名
  • 使用while循环而不是for循环
  • 缺乏代码模块化

让我们从最后一条评论开始,缺乏模块化,并解决其他问题。确定一个数独网格是否有效实际上非常复杂,它有三个组成部分。 1所有行的位数都正确吗? 2所有列的位数都正确吗? 三。整个网格的位数正确吗?你知道吗

3实际上是1和2的一个因子,你在你的代码中发现了这一点!但是如果我们把第一和第二部分分解成各自的函数,可能会使事情更容易阅读。那些看起来怎么样?我们先划船吧。对于我们的函数,我们将检查每一行并确认它的位数正确。你知道吗

让我们从行检查器开始。我们要做的就是:

def has_correct_number_rows(sudo_board):
    # the set we create below is an unordered grouping of the numbers
    # 1-4.
    correct_set = set(range(1, len(sudo_board)))
    for row in sudo_board:
        # this if statement checks if our row contains values 1-4
        if (correct_set != set(row)):
            return False
    return True

如果所有行都包含正确的项数,则返回True,否则返回false。你知道吗

接下来,检查列数是否正确。这稍微复杂一些,但仍然相当简单:

def has_correct_number_cols(sudo_board):
    correct_set = set(range(1, len(sudo_board) + 1))
    for col_num in range(0, len(sudo_board)):
        # col_set creates a set of the elements in a given column
        col_set = set([row[col_num] for row in sudo_board])
        if (correct_set != set(row)):
            return False
    return True

这里的返回值相同。你知道吗

把它们放在一起

现在您有了这两个函数,您的最终检查实际上非常简单。具体如下:

def sudoku_check_peter(sudo_board):
    correct_rows = has_correct_number_rows(sudo_board)
    correct_cols = has_correct_number_cols(sudo_board)
    # This last line returns True if both are true, otherwise
    # False.
    return correct_rows and correct_cols

这最终是相当冗长,我很抱歉很高兴回答后续问题或解释更多!希望这有帮助。你知道吗

相关问题 更多 >