Start learning free

LESSON 206 OF 291 · PROJECTS: EVERYDAY PROGRAMS

Report Card V6 in Kotlin: Highest, Lowest and Average of a List

Practice Read the full explanation

Compute three class statistics from a list of scores: the highest, the lowest, and the average.

You start with:

val scores = listOf(92, 78, 85, 67, 74, 88, 91, 58)

Build:

Highest: 92
Lowest: 58
Average: 79

Rules:

  • highest is the largest value in scores.
  • lowest is the smallest value in scores.
  • average is the sum of all scores divided by how many there are, rounded down (integer division).
  • Print "Highest: ", "Lowest: " and "Average: " on three lines, in the order of the sample output (println("Highest: $highest")).

Use scores.max() and scores.min() on the list; sum with a loop.

val scores = listOf(92, 78, 85, 67, 74, 88, 91, 58)

val highest = scores.max()
val lowest = scores.min()
var total = 0
for (s in scores) {
    total += s
}
val average = total / scores.size

println("Highest: $highest")
println("Lowest: $lowest")
println("Average: $average")

STEP 1 OF 9
    Ctrl + Enter to check

    KOTLIN · PROJECTS: EVERYDAY PROGRAMS

    Report Card V6 in Kotlin, explained

    Problem overview

    The last version of the project computes class statistics. From a list of scores the program prints the highest score, the lowest score and the average, rounded down. The starter code is one line:

    val scores = listOf(92, 78, 85, 67, 74, 88, 91, 58)
    

    The highest is 92 and the lowest is 58. The eight scores add up to 633, and 633 divided by 8 is 79.125, so with integer division the average is 79:

    Highest: 92
    Lowest: 58
    Average: 79
    

    Approach

    There are three small jobs. The highest and the lowest values are built into Kotlin's collections: scores.max() and scores.min() each return the answer in a single call, so no loop is needed for them.

    The average needs a total, and the lesson adds it up by hand with a loop. Start a counter at 0, add every score to it, and when the loop ends divide by the number of scores. Both sides of the division are Int, so the result drops the fraction, as in the first versions of the project.

    Step by step

    1. Highest. Declare val highest = scores.max(). The list was made with listOf, so it is read-only, but reading its biggest element is fine.
    2. Lowest. Declare val lowest = scores.min(), the mirror of the previous call.
    3. Total. Declare var total = 0. It must be a var, because it changes as scores are added.
    4. Loop over the scores. for (s in scores) hands you one score per pass, in list order. Close its brace right away.
    5. Add to the total. Inside the loop, total += s.
    6. Average. After the loop, val average = total / scores.size. The loop has finished, so total is the full sum.
    7. Print three lines. Each one is the label, then the value: Highest: , Lowest: and Average: .

    The solution in Kotlin

            val scores = listOf(92, 78, 85, 67, 74, 88, 91, 58)
            val highest = scores.max()
            val lowest = scores.min()
            var total = 0
            for (s in scores) {
                total += s
            }
            val average = total / scores.size
            println("Highest: $highest")
            println("Lowest: $lowest")
            println("Average: $average")
    

    Kotlin details worth knowing:

    • max() and min() return an Int. In current Kotlin they return the element itself, not a nullable value. If you need a result that is null for an empty list, maxOrNull() and minOrNull() do that.
    • listOf is read-only. It gives a List<Int> that you can read but not change: there is no add on it. A MutableList, made with mutableListOf, can change.
    • scores.size is a property. A list's size is written without parentheses, scores.size.
    • += on a var. total += s is shorthand for total = total + s. It works only because total is a var.
    • The loop is a choice. scores.sum() returns the same 633 in one call. The lesson writes the loop so that you practise the accumulator pattern, which you will need when there is no ready-made function.

    Following the program

    The running total grows by one score per pass: 92, then 170, 255, 322, 396, 484, 575 and finally 633. After the loop, average is 633 / 8. The mathematical value is 79.125, and the Int division keeps only the 79. Converting the total to Double first, total.toDouble() / scores.size, would give 79.125, but the lesson asks for the whole number.

    Edge cases and common mistakes

    • An empty list. emptyList<Int>().max() throws a NoSuchElementException, and 0 / list.size throws an ArithmeticException with the message / by zero. The lesson's data always has scores, so it doesn't guard against this.
    • One score. For listOf(70) the highest, the lowest and the average are all 70.
    • Negative values. Starting a hand-written search for the biggest value at 0 is a trap: for the list -3, -9 it would report 0, which isn't in the list. That is one reason to prefer max(), which returns -3.
    • Truncation. The scores 1, 2 and 2 give an average of 1, not 2, because 5 divided by 3 is truncated.
    • val for the total. A val total can't be changed in the loop, and the compiler rejects total += s. The accumulator has to be a var.

    Testing

    scoresOutput
    92, 78, 85, 67, 74, 88, 91, 58Highest: 92, Lowest: 58, Average: 79
    70Highest: 70, Lowest: 70, Average: 70
    1, 2, 2Highest: 2, Lowest: 1, Average: 1
    Keep going with Kotlin1,600+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free