【问题标题】:Checking Column in Matrix for Duplicate and Performing a Number Switch - Sudoku - Python检查矩阵中的列是否重复并执行数字切换 - 数独 - Python
【发布时间】:2020-07-20 04:31:06
【问题描述】:

因此,我将其作为个人项目进行,但在获得我想要的结果时遇到了一些困难。

def createSudoku():
    rowCount = 9
    columnCount = 9
    gameBoard = []

    ##Row entry is randomized order
    ##Column entry is not randomized order
    ##Row entry cannot be duplicate
    for row in range(rowCount):
        rowList = []
        sudokuNumbers = [1, 2, 3, 4, 5, 6, 7, 8, 9]
        for column in range(columnCount):
            choice = random.choice(sudokuNumbers)
            rowList.append(choice)
            sudokuNumbers.remove(choice)
        gameBoard.append(rowList)

因此,我的代码的上述部分当前是随机创建我的行,这是我的意图。现在,我开始用另一个循环搜索矩阵,以识别列实际上是随机的。

    K = 0
    ##Column Duplicate Algorithm
    while(K < columnCount):
        for sub in gameBoard:
            columnK = sub[K]
        sudokuNumbers = [1, 2, 3, 4, 5, 6, 7, 8, 9]
        for item in range(columnK):
            if item in sudokuNumbers:
                sudokuNumbers.remove(item)
            else:
                sub.append(sub.pop(sub.index(item)))
                if ...            

        K += 1

我对列搜索的意图是利用列的位置 [item] 和行位置 [K],并在该行上弹出/移动另一个条目来代替重复项,重新检查并继续。我在修补它之后现在看到的问题是,我最初的想法是使用循环或递归执行它,但这将是一个相当大的兔子洞。我很好奇我在这种情况下的选择,以确定如何替换行上的条目,还是我在这里过度复杂化了解决方案?

我知道,为了更好的 OOP,我应该专注于将循环分离为它们自己的函数,我的意图是这样做,我只是在检查我的数独时卡住了。

【问题讨论】:

    标签: python loops duplicates sudoku


    【解决方案1】:

    此循环将为您找到重复项并将其消失。

    exclusions = [43606,125490,...]
    def ID_Generator(new_clients): # This is a list of new client
    
      new_client_IDs = []
    
      while len(new_client_IDs) < len(new_clients):
        ID = randint(40000, 200000)
        if ID not in exclusions:
          new_client_IDs.append(ID)
        if list(dict.fromkeys(new_client_IDs)):
    
          new_client_IDs = list(dict.fromkeys(new_client_IDs))
    

    【讨论】:

      猜你喜欢
      • 2021-03-05
      • 2019-03-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-11-10
      • 1970-01-01
      相关资源
      最近更新 更多