Free practice · ordering · medium

Practice

Process a list safely

Order a simple workflow that calculates the mean of a non-empty list.

Collection-processing contract

A mean needs valid input, changing state, and one final division

Input rule
The list must contain at least one value
Changing state
total and count
Per-item rule
Add one value and increase count once
Required output
One mean after all items are visited

The trace should show why division is safe and how total and count stay synchronized as the list is processed.

Arrange in the correct order
  1. 1. Divide total by count
  2. 2. Add each value and increment count
  3. 3. Initialize total and count
  4. 4. Confirm the list is non-empty