TL ; DR:
list.min() gives the smallest value of a list.
list.max() gives the largest value.
list.sum() adds all the values up.
val prices = mutableListOf(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. Kotlin lists have them built in.
list.min()gives the smallest value of the list.list.max()gives the largest value.list.sum()adds all the values up.
maxOf(a, b) from Maximum Number In Array compares two values. max() looks at a whole list.
Watch out for an empty list. min() and max() throw a NoSuchElementException on it. An empty list has no smallest value. minOrNull() and maxOrNull() give null instead. The Elvis operator from Null Safety adds a fallback. So list.minOrNull() ?: 0 gives 0 for an empty list. sum() of an empty list 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.
