-
Notifications
You must be signed in to change notification settings - Fork 10
/
Contents.swift
98 lines (87 loc) · 2.57 KB
/
Contents.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
import UIKit
// Questions Review: Optionals and Dictionaries
// Optionals
/*
Question 6
a. Given the variable numbers below, write code that prints "The sum of all the numbers is "
followed by their sum. If a number is nil, don't add it to the sum. If all numbers are nil,
the sum is zero.
var numbers = [Int?]()
for _ in 0..<10 {
numbers.append(Bool.random() ? Int.random(in: 0...100) : nil)
}
b. Using the same variable, find the average of all non-nil values.
*/
var messages: [String] = ["Good, Morning"]
for _ in 0..<10 {
messages.append("Hello, Yulia") // append() - adds to array
}
print(messages.count)
// a - add only non-nil values
var numbers = [Int?]()
for _ in 0..<10 {
numbers.append(Bool.random() ? Int.random(in: 0...100) : nil)
}
var sum = 0
for num in numbers {
sum += num ?? 0 // nil-coalescing to unwrap num
}
print("The sum of all the numbers is \(sum)")
// b - average of non-nil values
sum = 0 // clearing sum
var nonNilValueCount = 0
for num in numbers {
// use optional binding to unwrap num
if let unwrappedNum = num {
// valid integer here, increment nonNilValueCount by 1
nonNilValueCount += 1
sum += unwrappedNum
}
}
print(numbers)
print("The average of the \(nonNilValueCount) non-nil values is \(sum / nonNilValueCount)")
// Dictionaries
// Question Four
// You are given an array of dictionaries. Each dictionary in the array describes the score of a person. Find the person with the highest score and print his / her full name.
var peopleWithScores: [[String: String]] = [
[
"firstName": "Calvin",
"lastName": "Newton",
"score": "13"
],
[
"firstName": "Garry",
"lastName": "Mckenzie",
"score": "23"
],
[
"firstName": "Leah",
"lastName": "Rivera",
"score": "10"
],
[
"firstName": "Sonja",
"lastName": "Moreno",
"score": "3"
],
[
"firstName": "Noel",
"lastName": "Bowen",
"score": "16"
]
]
var highestScore = 0
var fullName = ""
for currentPersonDictionary in peopleWithScores {
print("currently looking at \(currentPersonDictionary["firstName"] ?? "")")
let scoreAsString = currentPersonDictionary["score"] ?? "0"
let scoreAsInt = Int(scoreAsString) ?? 0
if scoreAsInt > highestScore { // e.g 13 > 0
highestScore = scoreAsInt // e.g 13
let firstName = currentPersonDictionary["firstName"] ?? "Jane"
let lastName = currentPersonDictionary["lastName"] ?? "Doe"
fullName = firstName + " " + lastName
}
print("\n")
}
print("\(fullName) has the highest score of \(highestScore)")