Before drawing anything, compute the summary numbers that a chart will later show to be incomplete.
What to build
A program that prints the count, mean, median and standard deviation of a list of numbers.
Requirements
- Print
count: N,mean: M,median: Dandstandard deviation: S, the last three with two decimal places. - Use the population standard deviation: divide the sum of squared differences from the mean by the count.
Input
One line of numbers separated by spaces.
Output
Four lines.
Constraints
1 to 1000 numbers, each with absolute value at most 10^6.
Example
Input:
2 4 4 4 5 5 7 9
Output:
count: 8
mean: 5.00
median: 4.50
standard deviation: 2.00
Why it works
These are the numbers most reports quote; knowing how each is computed makes it clear what each can and cannot say about the shape of the data.
Examples
Example 1 · Input
2 4 4 4 5 5 7 9
Expected output
count: 8 mean: 5.00 median: 4.50 standard deviation: 2.00
Example 2 · Input
10
Expected output
count: 1 mean: 10.00 median: 10.00 standard deviation: 0.00
Prompts inside input() are ignored when checking, so input("n: ") is fine.
Hints
Hint 1
The median of an even number of values is the mean of the two middle ones.
Hint 2
The population standard deviation is the square root of the mean squared distance from the mean.
Solution
Show one correct solution (try it yourself first)
values = [float(v) for v in input().split()]
n = len(values)
mean = sum(values) / n
ordered = sorted(values)
middle = n // 2
median = ordered[middle] if n % 2 else (ordered[middle - 1] + ordered[middle]) / 2
sd = (sum((v - mean) ** 2 for v in values) / n) ** 0.5
print(f"count: {n}")
print(f"mean: {mean:.2f}")
print(f"median: {median:.2f}")
print(f"standard deviation: {sd:.2f}")