Data science: Fill in missing values

Some measurements were not recorded. Fill the gaps with the mean of the values that were.

What to build

A program that fills missing values with the mean.

Requirements

  • A value is missing when its line is empty or NA (after removing spaces).
  • Print missing: K, then mean of the rest: X (two decimals), then all n values on one line, separated by spaces, each with one decimal, with the missing ones replaced by the mean.

Input

Line 1: n. Then n lines, each a number, NA or empty.

Output

Three lines.

Constraints

1 ≤ n ≤ 1000; at least one value is present.

Example

Input:

5
4
NA
6

8

Output:

missing: 2
mean of the rest: 6.00
4.0 6.0 6.0 6.0 8.0

Why it works

Filling with the mean of the known values keeps the column's average unchanged, which is why it is the usual first choice for symmetric data, and counting the gaps tells the reader how much of the result is estimated.

Examples

Example 1 · Input
5
4
NA
6

8
Expected output
missing: 2
mean of the rest: 6.00
4.0 6.0 6.0 6.0 8.0
Example 2 · Input
3
1.5
2.5
3
Expected output
missing: 0
mean of the rest: 2.33
1.5 2.5 3.0

Prompts inside input() are ignored when checking, so input("n: ") is fine.

Hints

Hint 1

Treat a line that is empty or NA (after strip) as missing, and keep None for it.

Hint 2

Compute the mean from the present values only, then replace each None with it.

Solution

Show one correct solution (try it yourself first)
n = int(input())
values = []
for _ in range(n):
    text = input().strip()
    values.append(None if text in ("", "NA") else float(text))
present = [v for v in values if v is not None]
mean = sum(present) / len(present)
print("missing:", n - len(present))
print(f"mean of the rest: {mean:.2f}")
print(" ".join(f"{(mean if v is None else v):.1f}" for v in values))

Lesson: Cleaning data →