【问题标题】:How to format a 2d array to fit a Soduku board from an input file. Python如何格式化二维数组以适应输入文件中的数独板。 Python
【发布时间】:2021-04-29 20:24:48
【问题描述】:

所以我目前在格式化从数据文件中获得的 9x9 板时遇到了一些麻烦。我应该做的是从输入文件中读取数据,以类似于数独谜题的方式排列数据。稍后我需要创建一个函数,读取每个 3x3 块,如果它是有效块,则返回 True(这意味着 1-9 中的数字不会重复。

我的代码适用于来自输入文件的单个 3x3 数据块,但不适用于更大的 9x9 文件。

这是文件

9
1 2 3 3 2 1 1 2 3
4 5 6 5 6 4 6 5 4
7 8 9 9 8 7 8 7 9
1 2 3 3 2 1 1 2 3
4 5 6 5 6 4 6 5 5
7 8 9 9 8 7 8 7 9
1 2 3 3 2 1 1 2 3
4 5 6 5 6 4 6 5 4
7 8 9 9 8 7 8 7 9

我的函数读取数据并存储在数组中

import numpy as np


def read_file(file_name):
    """
    :param file_name: Is the name of a text file, you do not need to enter the extention ".txt"
    :return: returns an array that encapsulates all the data from the input files
    """
    f = open(file_name, "r")
    data = f.read().split()
    organized_data = np.array(data)
    organized_data = organized_data.astype(np.int)
    return organized_data

以及应该转换成类似于数独游戏的数组的代码 sn-p

a = read_file("simplified_sudoku_provideddata/sudoku5.txt")
print(a)


newarr = a[1:].reshape(a[0], a[0])
print(newarr)

这给了我

[[1 2 3 3 2 1 1 2 3]
 [4 5 6 5 6 4 6 5 4]
 [7 8 9 9 8 7 8 7 9]
 [1 2 3 3 2 1 1 2 3]
 [4 5 6 5 6 4 6 5 5]
 [7 8 9 9 8 7 8 7 9]
 [1 2 3 3 2 1 1 2 3]
 [4 5 6 5 6 4 6 5 4]
 [7 8 9 9 8 7 8 7 9]]

我在想出我得到的解决方案时遇到了麻烦

[[1 2 3 4 5 6 7 8 9],
 [3 2 1 5 6 4 9 8 7],
 [1 2 3 6 5 4 9 7 9],
 [1 2 3 4 5 6 7 8 9],
 [3 2 1 6 5 4 9 8 7],
 [1 2 3 6 5 5 8 7 9],
 [1 2 3 4 5 6 7 8 9],.. and so on

是我的第一个函数中的错误吗?

【问题讨论】:

    标签: python arrays sudoku


    【解决方案1】:

    我不确定是否使用 numpy,但您可以尝试一下,然后将其转换为数组:

    # import numpy as np (no longer needed for my code)
    
    def read_file(file_name):
        """
        :param file_name: Is the name of a text file, you need to enter the extension ".txt"
        :return: returns an array that encapsulates all the data from the input files
        """
    
        return [int(n.strip()) for n in open(file_name, "r").read().split()] # smaller and more compact
    
    def split(lst, n): # this entire function is adapted code from another stackoverflow user
        """
        :param lst: is the list to be split evenly into n chunks
        :param n: is the int of chunks lst is to be split into
        :return: returns a list of n lists containing chunks of lst
        """
    
        quotient, remainder = divmod(len(iterable), n)
        output = []
        stop = 0
        for i in range(n):
            start = stop
            stop += quotient + 1 if i + 1 <= remainder else quotient
            output.append(list(iter(iterable[start:stop])))
        return output
    
    a = read_file("simplified_sudoku_provideddata/sudoku5.txt")
    print(a) # note that this will include the '9' at the start (which I assume indicates the size of the square)
    print()
    newarr = split(a[1:], a[0])
    print(newarr)
    

    这应该给你:

    [9,1,2,3,3,2,1,1,2,3,4,5,6,5,6,4,6,5,4,7,8,9,9,8,7,8,7,9,1,2,3,3,2,1,1,2,3,4,5,6,5,6,4,6,5,5,7,8,9,9,8,7,8,7,9,1,2,3,3,2,1,1,2,3,4,5,6,5,6,4,6,5,4,7,8,9,9,8,7,8,7,9]
    
    [[1,2,3,3,2,1,1,2,3],[4,5,6,5,6,4,6,5,4],[7,8,9,9,8,7,8,7,9],[1,2,3,3,2,1,1,2,3],[4,5,6,5,6,4,6,5,5],[7,8,9,9,8,7,8,7,9],[1,2,3,3,2,1,1,2,3],[4,5,6,5,6,4,6,5,4],[7,8,9,9,8,7,8,7,9]]
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-04-07
      • 2019-04-05
      • 2019-09-02
      • 2020-08-20
      • 1970-01-01
      相关资源
      最近更新 更多