@@ -31,7 +31,10 @@ object Grade:
31
31
* - `apply` to select an element at a specific index.
32
32
*/
33
33
def median (grades : Seq [Grade ]): Grade =
34
- ???
34
+ grades.sortBy(_.ordinal)
35
+ .drop(grades.length / 2 )
36
+ .head
37
+
35
38
end Grade
36
39
37
40
/**
@@ -73,14 +76,14 @@ case class Election(description: String, candidates: Set[Candidate]):
73
76
// into a single sequence containing the grades assigned to each
74
77
// candidate by the voters.
75
78
val allGrades : Seq [(Candidate , Grade )] =
76
- ???
79
+ ballots.flatMap(_.grades.toSeq)
77
80
78
81
// Second step: use the operation `groupMap` to transform the
79
82
// collection of pairs of `(Candidate, Grade)` into a `Map`
80
83
// containing all the grades that were assigned to a given
81
84
// `Candidate`.
82
85
val gradesPerCandidate : Map [Candidate , Seq [Grade ]] =
83
- ???
86
+ allGrades.groupMap(_._1)(_._2)
84
87
85
88
findWinner(gradesPerCandidate)
86
89
end elect
@@ -108,18 +111,21 @@ case class Election(description: String, candidates: Set[Candidate]):
108
111
// of grades, and finally use the operation `maxBy` to find the highest
109
112
// median grade.
110
113
val bestMedianGrade : Grade =
111
- ???
114
+ gradesPerCandidate.values
115
+ .filter(_.nonEmpty)
116
+ .map(Grade .median)
117
+ .maxBy(_.ordinal)
112
118
113
119
// Use the operation `filter` to select all the candidates that got the
114
120
// same best median grade (as the case may be)
115
121
val bestCandidates : Map [Candidate , Seq [Grade ]] =
116
- ???
122
+ gradesPerCandidate.filter((_, grades) => Grade .median(grades) == bestMedianGrade)
117
123
118
124
// In case only one candidate got the best median grade, it’s the winner!
119
125
if bestCandidates.size == 1 then
120
126
// Use the operation `head` to retrieve the only element
121
127
// of the collection `bestCandidates`
122
- ???
128
+ bestCandidates.head._1
123
129
else
124
130
// Otherwise, there is a tie between several candidates. The tie-breaking
125
131
// algorithm is the following:
@@ -128,16 +134,16 @@ case class Election(description: String, candidates: Set[Candidate]):
128
134
// median grade from each tied candidate's total. This is repeated until only one
129
135
// of the previously tied candidates is currently found to have the highest
130
136
// median-grade.” (source: https://en.wikipedia.org/wiki/Majority_judgment)
131
-
137
+
132
138
// Use the operation `map` to transform each element of the `bestCandidates`.
133
139
// And use the operation `diff` to remove one `bestMedianGrade` from the
134
140
// grades assigned to the candidates.
135
141
val bestCandidatesMinusOneMedianGrade : Map [Candidate , Seq [Grade ]] =
136
- ???
137
-
142
+ bestCandidates.map((candidate, grades) => (candidate, grades diff Seq (bestMedianGrade)))
143
+
138
144
// Finally, call `findWinner` on the reduced collection of candidates,
139
145
// `bestCandidatesMinusOneMedianGrade`.
140
- ???
146
+ findWinner(bestCandidatesMinusOneMedianGrade)
141
147
end findWinner
142
148
143
149
end Election
0 commit comments