gpt4 book ai didi

swift - "unknown error code 132"与 IBM Swift 沙箱

转载 作者:行者123 更新时间:2023-11-30 10:08:56 24 4
gpt4 key购买 nike

(我对此还很陌生,因此非常感谢有关本文一般形式和我的代码的提示!)

<小时/>

我一直在 IBM Sandbox 中使用 Swift ,我似乎无法解决以下问题:

func fillPossibilityMatrix() {        //it's a 9x9 Matrix
for i in 0...80 {
let row = (i - (i % 9)) / 9 //-> row is Valid for 0 - 8
let column = i % 9
if possibilityMatrix[row, column] == [0] {
possibilityMatrix[row, column] = possibilities(row, column: column)
}
}

这给了我 132 未知错误!

  • 尽管我可以使用此处使用的每种值组合来调用 possibilityMatrixpossibilities(),但只要我在它们之间添加“=”,事情变得很奇怪。

  • 当我尝试将值分配给数组/矩阵的无效索引时,我之前见过 132 错误,但我在这里没有看到...

-以下工作得很好。 (请注意“prints”而不是“=”)

func fillPossibilityMatrix() {
对于我在 0...80 {
让行 = (i - (i % 9))/9
让列 = i % 9
如果可能性矩阵[行,列] == [0] {
print(可能性矩阵[行,列])
print(可能性(行,列:列))
}
}
}

  • 当我为循环设置不同的范围时,它也有效。但决定是否使用的既不是特定值,也不是范围的大小。

这里出了什么问题?我只是愚蠢吗?这是 IBM 网站特有的吗?

<小时/>

其余的

(我试图让它解决数独)

-

possibilityMatrix 是这样产生的:(这里:字段<->可能性矩阵)

struct Matrix {
let rows: Int, columns: Int
var grid: [[Int]]
init(rows: Int, columns: Int) {
self.rows = rows
self.columns = columns
grid = Array(count: rows * columns, repeatedValue: [0])
}
func indexIsValidForRow(row: Int, column: Int) -> Bool {
return row >= 0 && row < rows && column >= 0 && column < columns
}
subscript(row: Int, column: Int) -> [Int] {
get {
assert(indexIsValidForRow(row, column: column), "Index out of range")
return grid[(row * columns) + column]
}
set {
assert(indexIsValidForRow(row, column: column), "Index out of range")
grid[(row * columns) + column] = newValue
}
}
}
var inputArray = [Int!] ()
var input = "003020600900305001001806400008102900700000008006708200002609500800203009005010300"
var field = Matrix(rows: 9, columns: 9)

for char in input.characters {
inputArray.append(Int(String(char)))
}
func fromInputToField() {
for i in 0..<inputArray.count {
let row = (i - (i % 9))/9
let column = i % 9
field[row, column][0] = (inputArray[i])
}
}
fromInputToField()

var possibilityMatrix = field

-

possibilities() 及其子函数如下所示:

func possibilities(row: Int, column: Int) -> [Int] {
let numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9]
return numbers.filter {
!rowContains(row, number: $0) && !columnContains(column, number: $0) && !boxContains(row, c: column, number: $0)
}
}


func rowContains(r: Int, number: Int) -> Bool {
for i in 0...8 {
if possibilityMatrix[r, i][0] == number {
return true
}
}
return false
}
func columnContains(c: Int, number: Int) -> Bool {
for i in 0...8 {
if possibilityMatrix[i, c][0] == number {
return true
}
}
return false
}
func boxContains (r: Int, c: Int, number: Int) -> Bool {
let boxLocation = locateBox(r, c: c)
for x in 0...2 {
for y in 0...2 {
if possibilityMatrix[boxLocation.0 + y, boxLocation.1 + x][0] == number {
return true
}
}
}
return false
}

func locateBox (r: Int, c: Int) -> (upBorder: Int, leftBorder: Int) {
if r % 3 != 0 {
return locateBox(r - 1, c: c)
}
if c % 3 != 0 {
return locateBox(r, c: c - 1)
}
return (r, c)
}
<小时/><小时/><小时/>

用于复制粘贴

struct Matrix {
let rows: Int, columns: Int
var grid: [[Int]]
init(rows: Int, columns: Int) {
self.rows = rows
self.columns = columns
grid = Array(count: rows * columns, repeatedValue: [0])
}
func indexIsValidForRow(row: Int, column: Int) -> Bool {
return row >= 0 && row < rows && column >= 0 && column < columns
}
subscript(row: Int, column: Int) -> [Int] {
get {
assert(indexIsValidForRow(row, column: column), "Index out of range")
return grid[(row * columns) + column]
}
set {
assert(indexIsValidForRow(row, column: column), "Index out of range")
grid[(row * columns) + column] = newValue
}
}
}
var inputArray = [Int!] ()
var input = "003020600900305001001806400008102900700000008006708200002609500800203009005010300"
var field = Matrix(rows: 9, columns: 9)

for char in input.characters {
inputArray.append(Int(String(char)))
}
func fromInputToField() {
for i in 0..<inputArray.count {
let row = (i - (i % 9))/9
let column = i % 9
field[row, column][0] = (inputArray[i])
}
}
fromInputToField()

var possibilityMatrix = field
func possibilities(row: Int, column: Int) -> [Int] {
let numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9]
return numbers.filter {
!rowContains(row, number: $0) && !columnContains(column, number: $0) && !boxContains(row, c: column, number: $0)
}
}


func rowContains(r: Int, number: Int) -> Bool {
for i in 0...8 {
if possibilityMatrix[r, i][0] == number {
return true
}
}
return false
}
func columnContains(c: Int, number: Int) -> Bool {
for i in 0...8 {
if possibilityMatrix[i, c][0] == number {
return true
}
}
return false
}
func boxContains (r: Int, c: Int, number: Int) -> Bool {
let boxLocation = locateBox(r, c: c)
for x in 0...2 {
for y in 0...2 {
if possibilityMatrix[boxLocation.0 + y, boxLocation.1 + x][0] == number {
return true
}
}
}
return false
}

func locateBox (r: Int, c: Int) -> (upBorder: Int, leftBorder: Int) {
if r % 3 != 0 {
return locateBox(r - 1, c: c)
}
if c % 3 != 0 {
return locateBox(r, c: c - 1)
}
return (r, c)
}
func fillPossibilityMatrix() { //it's a 9x9 Matrix
for i in 0...80 {
let row = (i - (i % 9)) / 9 //-> row is Valid for 0 - 8
let column = i % 9
if possibilityMatrix[row, column] == [0] {
possibilityMatrix[row, column] = possibilities(row, column: column)
}
}
}
fillPossibilityMatrix()

最佳答案

事实证明这个问题非常简单。以下是您可能的功能:

func rowContains(r: Int, number: Int) -> Bool {
for i in 0...8 {
if possibilityMatrix[r, i][0] == number {
return true
}
}
return false
}
func columnContains(c: Int, number: Int) -> Bool {
for i in 0...8 {
if possibilityMatrix[i, c][0] == number {
return true
}
}
return false
}
func boxContains (r: Int, c: Int, number: Int) -> Bool {
let boxLocation = locateBox(r, c: c)
for x in 0...2 {
for y in 0...2 {
if possibilityMatrix[boxLocation.0 + y, boxLocation.1 + x][0] == number {
return true
}
}
}
return false
}

问题是您正在检查probabilityMatrix以查看该数字是否存在 - 这是您正在修改的变量。因此,如果您将 probabilityMatrix[0, 0] 更改为等于 [4, 5],那么当您检查 probabilityMatrix[0, 1] 时,由于您刚刚进行的更改,您的函数将假定 4 位于第一行。其检查的一部分是查看 probabilityMatrix[0, 0] 的第一个元素,即 4,因此您的代码认为第一行自然有一个 4 其中。

经过足够多的操作后,您最终将到达一个正方形,其中可能性列表为[],因为错误的数字随着您沿着行(或列或框)前进而累积,最终所有的可能性都消失了。然后在下一次传递中,像 rowContains 这样的函数将查看该 [] 并尝试从中获取第一个元素(如 possibilityMatrix[r, i][0]),它不存在。这就是导致索引超出范围错误的原因。

解决方案是与 field 进行比较,而不是与 possibilityMatrix 进行比较,因为该变量永远不会改变并且始终保存原始矩阵。所以你的函数应该是这样的:

func rowContains(r: Int, number: Int) -> Bool {
for i in 0...8 {
if field[r, i][0] == number {
return true
}
}
return false
}
func columnContains(c: Int, number: Int) -> Bool {
for i in 0...8 {
if field[i, c][0] == number {
return true
}
}
return false
}
func boxContains (r: Int, c: Int, number: Int) -> Bool {
let boxLocation = locateBox(r, c: c)
for x in 0...2 {
for y in 0...2 {
if field[boxLocation.0 + y, boxLocation.1 + x][0] == number {
return true
}
}
}
return false
}

这是在沙盒中实现的工作版本供您查看:

http://swiftlang.ng.bluemix.net/#/repl/6fe779409351531ce07d3bc3f0c197639538729b40d5f0f658e4dd53985223fe

编辑:虽然学习递归是一件好事,但如果可以的话,最好避免它,因为它比迭代循环占用更多的资源(因为每次调用函数时,它都会被放置在程序堆栈上并需要资源)。这是达到相同结果的更快方法:

func locateBox (r: Int, c: Int) -> (upBorder: Int, leftBorder: Int) {
return ((r - (r % 3)), (c - (c % 3)))
}

此外,您的 fillPossibilityMatrix() 函数作为双循环可能会更好地工作,因为 1) 除法和求余函数的计算量有点大,2) 它更易于阅读。

func fillPossibilityMatrix() {        //it's a 9x9 Matrix
for row in 0...8 {
for column in 0...8 {
if possibilityMatrix[row, column] == [0] {
possibilityMatrix[row, column] = possibilities(row, column: column)
}
}
}
}

关于swift - "unknown error code 132"与 IBM Swift 沙箱,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34278918/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com