-
Notifications
You must be signed in to change notification settings - Fork 23
/
Contents.swift
48 lines (37 loc) · 1.17 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
import UIKit
// App Exercise - Step Competition
struct User {
var name: String
var stepsToday: Int
init(name: String, stepsToday: Int) {
self.name = name
self.stepsToday = stepsToday
}
init?(name: String?, stepsToday: Int?) {
if let name = name, let stepsToday = stepsToday {
self.name = name
self.stepsToday = stepsToday
} else {
return nil
}
}
}
let stepMaster = User(name: "StepMaster", stepsToday: 8394)
let activeSitter = User(name: "ActiveSitter", stepsToday: 9132)
let monsterWalker = User(name: "MonsterWalker", stepsToday: 7193)
let competitors = [stepMaster, activeSitter, monsterWalker]
func getWinner(competitors: [User]) -> User? {
var topCompetitor: User?
for competitor in competitors {
if let currentTopCompetitor = topCompetitor {
if competitor.stepsToday > currentTopCompetitor.stepsToday {
topCompetitor = competitor
}
} else {
topCompetitor = competitor
}
}
return topCompetitor
}
var topCompetitor = getWinner(competitors: competitors)
print(topCompetitor?.name)