【发布时间】: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
是我的第一个函数中的错误吗?
【问题讨论】: