Skip to content
ExamSpine

Lesson 3.10 · Write it

Stream statistics

A whole programPython or Java7 tests, 4 of them hidden

On a phone? Put the lines in order instead

Write a program that reads whole numbers one at a time, stopping at -1, and reports five statistics about the numbers it read.

  1. Read one number per line. The value -1 means "no more numbers" and is not counted as data.
  2. If at least one number was entered, print five lines in this order:
  • Count: followed by how many numbers were entered.
  • Sum: followed by their total.
  • Average: followed by the whole-number average (drop the fraction).
  • Min: followed by the smallest number.
  • Max: followed by the largest number.
  1. If -1 is typed first (no data), print No data.

All numbers are whole numbers from 0 to 1000. Your program is run several times. Only what it prints is checked.

In Python, use // for whole-number division. In Java, / between two ints already drops the fraction.

Running Java here comes later. You can put this answer's lines in order instead. You can still write your Java answer (it stays while this page is open) and compare it with our worked solution. Or try this exercise in Python.

Examples

Your program is run 7 times: the 3 examples below, and 4 hidden ones that check the rules above. Only what it prints is checked.

  • three numbers (rule 2)

    You type

    1. typed: 10
    2. typed: 20
    3. typed: 30
    4. typed: -1

    It prints

    Count: 3
    Sum: 60
    Average: 20
    Min: 10
    Max: 30
  • no data at all (rule 3)

    You type

    1. typed: -1

    It prints

    No data
  • one number, all stats equal (rule 2)

    You type

    1. typed: 5
    2. typed: -1

    It prints

    Count: 1
    Sum: 5
    Average: 5
    Min: 5
    Max: 5
solution.pySolution.java