## Question or problem in the Swift programming language:

I am working through the first chapter of the book and can’t figure out the experiment:

This is the code from the book:

let interstingNumbers = [ "Prime": [2, 3, 5, 7, 11, 13, 17], "Fibonacci": [1, 1, 2, 3, 5, 8], "Square": [1, 4, 9, 16, 25, 36], ] var largest = 0 for (kind, numbers) in interstingNumbers { for number in numbers { if number > largest { largest = number } } } largest

## How to solve the problem:

### Solution 1:

So basically you’d want to put `kind`

somewhere at the same time as you put `number`

into `largest`

, e.g. into a variable called `largestKind`

.

### Solution 2:

Like this:

let interstingNumbers = [ "Prime": [2, 3, 5, 7, 11, 13, 17], "Fibonacci": [1, 1, 2, 3, 5, 8], "Square": [1, 4, 9, 16, 25, 36], ] var largest = 0 var largestKind = "" for (kind, numbers) in interstingNumbers { for number in numbers { if number > largest { largest = number largestKind = kind } } } largest largestKind

### Solution 3:

The question asked to keep track of the value and type not to just display the type of the final answer.

I think this gets closer:

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 largestType = [String]() var largestValues = [Int]() for (kind, numbers) in interestingNumbers { for number in numbers { if number > largest { largest = number largestValues.append(largest) largestType.append(kind) } } } print(largest) print(largestType) print(largestValues)

And now with tuples:

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 largestValues: [(type: String, value: Int)] = [] for (kind, numbers) in interestingNumbers { for number in numbers { if number > largest { largest = number largestValues.append(type: kind, value: number) } } } print(largest) print(largestValues)

### Solution 4:

let interestingNumbers = [ "Prime" : [2,3,5,7,11,13], "Fibonacci" : [1,1,2,3,5,8,13], "Square" : [1,4,9,16,25,36], ] var largest = 0 var largestKind = "" for (kind, numbers) in interestingNumbers { for number in numbers { if number > largest { largest = number largestKind = kind } } } print(largest, largestKind) outputs : 36 Sqaure

### Solution 5:

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 largestNumbers = [ "Prime":0, "Fibonacci":0, "Square":0, ] for (kind, numbers) in interestingNumbers { for number in numbers { if number > largest { largest = number } if(number > largestNumbers[kind]!){ largestNumbers[kind] = number; } } } print(largest) print(largestNumbers) Result is: 25 ["Fibonacci": 8, "Square": 25, "Prime": 13]