W1349 Higher Order Functions

From Coder Merlin
Within these castle walls be forged Mavens of Computer Science ...
— Merlin, The Coder

Exercises[edit]

Professor Snape needs your help to organize ingredients for a wide variety of potions.  You'll need to verify blocks of code to ensure that they're doing exactly what Professor Snape needs.  Be careful!  Any mistakes can be hazardous to his health.  In some cases, more than one answer will be correct.

1 Consider the following code:

let x = ["apple", "banana", "carrot", "dill", "eggplant"]
var y = [String]()
for e in x {
     y.append(e)
} //

Which of the following produce identical results?
A

    let x = ["apple", "banana", "carrot", "dill", "eggplant"]
    var y = x.map {$0} //

B

    let x = ["apple", "banana", "carrot", "dill", "eggplant"]
    var y = [String]()
    for i in 0 ..< x.count {
        y.append(x[i])
    } //

C

    let x = ["apple", "banana", "carrot", "dill", "eggplant"]
    var y = [String]()
    for i in stride(from:0, to:x.count, by:1) {
        y.append(x[i])
    } //

D

    let x = ["apple", "banana", "carrot", "dill", "eggplant"]
    var y = [String]()
    for i in stride(from:0, through:x.count-1, by:1) {
        y.append(x[i])
    } //

A
B
C
D

2 Consider the following code:

    let x = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37]
    let f = {(accumulator:Double, currentElement:Int) -> Double in
        return accumulator - Double(currentElement)} //
    var y = x.reduce(0.0, f)

Which of the following produce identical results?
A

    let x = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37]
    let f = {(accumulator:Double, currentElement:Int) -> Double in
        return accumulator + Double(-currentElement)} //
    var y = x.reduce(0.0, f)

B

    let x = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37]
    var y = x.reduce(0.0, {$0 - Double($1)})

C

    let x = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37]
    var y = x.reduce(0.0) {$0 - Double($1)} //

D

    let x = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37]
    var y = x.reduce(0.0) {Double(-$1) + $0} //

A
B
C
D

3 Consider the following code:

    let x = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37]
    for i in stride(from:x.count-1, to:0, by:-2) {
        print(x[i])
    } //

Which of the following produce identical results?
A

    let x = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37]
    var isOther = true
    for i in stride(from:x.count-1, to:0, by:-1) {
        if (isOther) {
            print(x[i])
        } //
        isOther = !isOther
    } //

B

    let x = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37]
    var index = x.count - 1
    repeat {
        print(x[index])
        index -= 2
    } while (index >= 0)

C

    let x = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37]
    var index = x.count - 1
    while index >= 0 {
        print(x[index])
        index -= 2
    } //

D

    let x = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37]
    var index = x.count - 1
    var isOther = true
    while index >= 0 {
        if (isOther) {
            print(x[index])
        } //
        index -= 1
        isOther = !isOther
    } //

A
B
C
D

4 Consider the following code:

    let x = [45, 92, 95, 1014, 25, 83, 17, 19, 35]                                                                                                                                                                
    let y = x.map {$0 * 2}.filter {$0 % 5 == 0}.reduce(0, +)

Which of the following produce identical results?
A

    let x = [45, 92, 95, 1014, 25, 83, 17, 19, 35]
    let f1 = {(n:Int) -> Int  in return n * 2} //
    let f2 = {(n:Int) -> Bool in return n % 5 == 0} //
    let f3 = {(sum:Int, element:Int) -> Int in return sum + element} //
    let y = x.map(f1).filter(f2).reduce(0, f3)

B

    let x = [45, 92, 95, 1014, 25, 83, 17, 19, 35]
    var y = 0
    for e in x {
        let e2 = e * 2
        if e2 % 5 == 0 {
            y += e2
        } //
    } //

C

   let x = [45, 92, 95, 1014, 25, 83, 17, 19, 35]
    var y = 0
    for i in 0 ..< x.count {
        let e = x[i]
        let e2 = e * 2
        if e2 % 5 == 0 {
            y += e2
        } //
    } //

D

    let x = [45, 92, 95, 1014, 25, 83, 17, 19, 35]
    let f1 = {(n:Int) -> Int  in return n * 2} //
    let f2 = {(n:Int) -> Bool in return n % 5 == 0} //
    var y = 0
    for i in stride(from:x.count-1, through:0, by:-1) {
        let e2 = f1(x[i])
        if f2(e2) {
            y += e2
        } //
    } //

A
B
C
D