EXC_I386_GPFLT in Swift Code Using Xcode Playground

I ran the same code in an Xcode 7beta / rc Playground project and got an error:

Execution was aborted, reason: EXC_BAD_ACCESS (code = EXC_I386_GPFLT)

at

let n: Int = Int(Process.arguments[1])!

How can I solve in the Playground project, as other solutions do not seem to be related?

Binary tree: http://benchmarksgame.alioth.debian.org/u64q/program.php?test=binarytrees&lang=swift&id=1

class TreeNode {
    var left, right : TreeNode?
    var item : Int

    init(_ left: TreeNode?, _ right: TreeNode?, _ item: Int) {
        self.left = left
        self.right = right
        self.item = item
    }

    func check() -> Int {
        guard let left = left, let right = right else {
            return item
        }
        return item + left.check() - right.check()
    }
}

func bottomUpTree(item: Int, _ depth: Int) -> TreeNode {
    if depth > 0 {
        return
            TreeNode(
                bottomUpTree(2*item-1, depth-1),
                bottomUpTree(2*item, depth-1),
                item
        )
    }
    else {
        return
            TreeNode(nil,nil,item)
    }
}


let n: Int = Int(Process.arguments[1])!
let minDepth = 4
let maxDepth = n
let stretchDepth = n + 1

let check = bottomUpTree(0,stretchDepth).check()
print("stretch tree of depth \(stretchDepth)\t check: \(check)")

let longLivedTree = bottomUpTree(0,maxDepth)

var depth = minDepth
while depth <= maxDepth {
    let iterations = 1 << (maxDepth - depth + minDepth)
    var check = 0
    for i in 0..<iterations {
        check += bottomUpTree(i,depth).check()
        check += bottomUpTree(-i,depth).check()
    }
    print("\(iterations*2)\t trees of depth \(depth)\t check: \(check)")
    depth += 2
}

print("long lived tree of depth \(maxDepth)\t check: \(longLivedTree.check())")
+1
source share
2 answers

Process.arguments contains the value that is passed as arguments to the command line application.

: ( ), Process.arguments - , , Process.arguments[1].

, .

+2

readLine() , :

import Foundation

var currentLine = 0
let input = ["5", "5 6 3"]

func readLine() -> String? {
    if currentLine < input.endIndex {
        let line = input[currentLine]
        currentLine += 1
        return line
    } else {
        return nil
    }
}

let firstLine = readLine() //  5
let secondLine = readLine() // 5 6 3
let thirdLine = readLine() // nil
0

Source: https://habr.com/ru/post/1663081/


All Articles