Filling Gaps With the Mean
The model that forecasts flow at Milburn Weir needs a level for every hourly slot — it cannot be handed a hole. Northgate Water's rule for patching a hole is the mildest one available: stand the missing slot at the average of the readings that did arrive that day, so the patched day has the same average as the real readings and the gap adds no new information. The word nan in a slot is the logger's marker for a reading that never arrived; nan is short for Not a Number.
Task: Print the day's levels with every missing slot standing at the average of the readings that arrived.
Input
The first line holds an integer n, the count of slots. Each of the next n lines holds either a level written as a number, or the exact text nan. At least one slot holds a real level.
Output
One line holding n levels separated by single spaces, in the original slot order, each written with exactly two decimal places. The readings that arrived are printed unchanged apart from that formatting.
Example:
Input:
4
10
nan
20
30
Output:
10.00 20.00 20.00 30.00
Sign in to solve this problem
Reading problems is free for everyone — solving them (Run, Submit, and tracking what you've solved) needs an account.
Sign in