Swift for-in loop dictionary experiment
Asked Answered
A

3

23

I'm almost a complete programming beginner and I've started to go through a Swift ebook from Apple.

The things I read are pretty clear, but once you start to experiment things get tricky :).

I'm stuck with the experiment in the Control Flow section. Here is the initial code:

let interestingNumbers = [
    "Prime": [2, 3, 5, 7, 11, 13],
    "Fibonacci": [1, 1, 2, 3, 5, 8],
    "Square": [1, 4, 9, 16, 25],
]

var largest = 0
for (kind, numbers) in interestingNumbers {
    for number in numbers {
        if number > largest {
            largest = number
        }
    }
}

largest

And here is the task:

Add another variable to keep track of which kind of number was the largest, as well as what that largest number was.

As I understand, they want me to add up all the values in each number kind (get a total sum for Prime, Fibonacci and Square) and then compare the result to show the largest result. But I can't figure out the syntax.

Can someone share any advice on how to tackle this experiment? Maybe I'm not understanding the problem?

Alt answered 9/6, 2014 at 7:34 Comment(2)
It seems to be asking you to simply record the type of the number that is the largest (you're already tracking what the largest number is, that's what the largest variable is for).Inhospitality
It's a duplicate-question of https://mcmap.net/q/591611/-for-loop-in-apple-swift/…Arsonist
M
45

They're just asking you to keep track of which number category the largest number belongs to:

let interestingNumbers = [
    "Prime": [2, 3, 5, 7, 11, 13],
    "Fibonacci": [1, 1, 2, 3, 5, 8],
    "Square": [1, 4, 9, 16, 25],
]
var largest = 0
var largestkind = ""
for (kind, numbers) in interestingNumbers {
    for number in numbers {
        if number > largest {
            largest = number
            largestkind = kind
        }
    }
}
largest
largestkind
Munguia answered 9/6, 2014 at 7:53 Comment(3)
Thank you! It seems I misinterpreted the question.Alt
Could you also please help me understand the correct flow of events: 1. the code iterates through all the numbers in all the categories, 2. it finds the largest number, 3. it records the kind of the largest number. I'm I correct?Alt
Yes that's correct. As it goes through all the numbers it keeps reassigning values to largest/largestKind. For example it first sees 2 and assigns largest = 2, largestKind = "Prime". Then it sees 3 and since it's bigger it reassigns largest = 3, largestKind = "Prime".Munguia
D
0

Alternately you can use closure to make the tasks simpler.

The for loop calculate the sum of each series.

The final reduce finds the series tuple that contains maximum number.

let interestingNumbers = [
    "Prime": [2, 3, 5, 7, 11, 13],
    "Fibonacci": [1, 1, 2, 3, 5, 8],
    "Square": [1, 4, 9, 16, 25],
]

var sums =  Array<(String, Int)>()
for (kind, numbers) in interestingNumbers {
    sums = sums + [(kind, numbers.reduce(0, +))]
}

let maxSeries = sums.reduce(("", Int.min), { $0.1 > $1.1 ? $0 : $1 })

println(sums)
println(maxSeries)
Delmadelmar answered 9/6, 2014 at 8:28 Comment(3)
The task is to find the largest number. Not the sum.Jim
@jewirth - To quote OP: "As I understand, they want me to add up all the values in each number kind (get a total sum for Prime, Fibonacci and Square) and then compare the result to show the largest result. But I can't figure out the syntax."Delmadelmar
The OP misunderstood the actual task :-)Jim
C
0

Here it from playground using Xcode 8.3 and Swift 3.0

let interestingNumbers = [
"Prime": [2, 3, 5, 7, 11, 13],
"Fibonacci": [1, 1, 2, 3, 5, 8],
"Square": [1, 4, 9, 16, 25],
]

let largest = interestingNumbers.map{$0.value}.flatMap{$0}.max()
print(largest)

Optional(25)

Caseinogen answered 17/9, 2017 at 6:21 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.