TL ; DR:
buf.min gives the smallest value of a collection.
buf.max gives the largest value.
buf.sum adds all the values up.
val prices = scala.collection.mutable.ArrayBuffer(4, 9, 2, 7)
println(prices.min) // 2
println(prices.max) // 9
println(prices.sum) // 22
Full lesson:
In Maximum Number In Array, a loop found the largest value. In Sum of Numbers, a loop added numbers up. These jobs come up all the time. Scala collections have them built in.
buf.mingives the smallest value ofbuf.buf.maxgives the largest value.buf.sumadds all the values up.
They have no parentheses. They work the same on an ArrayBuffer, an Array or a List.
math.max(a, b) from Maximum Number In Array compares two values. buf.max looks at a whole collection.
Watch out for an empty collection. min and max throw an UnsupportedOperationException on it. An empty collection has no smallest value. minOption and maxOption give an Option instead, as in the Option lesson. So buf.minOption.getOrElse(0) gives 0 for an empty one. sum of an empty collection is 0.
Your task
minutes holds how many minutes you practiced coding on each day of a week.
Store the shortest day in shortest and the longest day in longest. Store the week's total in total.
Use the built-ins from this lesson. The code at the bottom prints all three.
Expected output:
shortest = 15
longest = 60
total = 235Free plan: 30 tutor messages. PRO is unlimited.
