Rainfall Summary
Turn a messy list of daily rainfall readings into the four numbers every report starts with: mean, median, mode and standard deviation.
Statement
Story
A weather station logs the rainfall (in millimetres) of every day of the season, in the order the readings arrived. Before the data goes into a dashboard, the analyst wants a one-glance summary.
Task
Read the readings and print four statistics, one per line, in this order:
- mean: the sum divided by the count
- median: the middle value of the sorted readings; for an even count, the average of the two middle values
- mode: the most frequent value; if several values share the highest frequency, print the smallest of them
- standard deviation: the population standard deviation,
sqrt( Σ(x − mean)² / n )(divide byn, notn − 1)
Input
- Line 1:
n(1 ≤ n ≤ 5000) - Line 2:
nnumbers, each with at most 2 decimal places and|x| ≤ 100000, in no particular order
Output
Four lines with the four numbers. Print at least 4 decimals; any answer within 0.0001 of the exact value is accepted.
Example
8
2 4 4 4 5 5 7 9
Output:
5
4.5
4
2
(The mean is 40 / 8 = 5, the two middle values 4 and 5 give 4.5, the value 4 appears three times, and the squared distances from 5 add up to 32, so σ = √(32 / 8) = 2.)
Sample input
8 2 4 4 4 5 5 7 9
Hints
Stuck? Hints are optional, and each one lowers the points you earn for solving this challenge.
- Hint 1Costs 5 points
- Hint 2Costs 5 points
Reveal the previous hint first
- Hint 3Costs 10 points
Reveal the previous hint first
Lessons that teach this
Stuck on the ideas rather than the puzzle? These lessons cover what you need.