Data science: Which group is more consistent?

Two groups can have similar averages and very different spreads. Compare how consistent they are, fairly, even when their averages differ.

What to build

A program that compares the spread of two groups with the coefficient of variation.

Requirements

  • For each group print NAME: mean M, median D, sd S, cv C%, with the mean, median and sample standard deviation to two decimals and the coefficient of variation (sd ÷ mean × 100) to one decimal.
  • Then print more consistent: NAME for the group with the smaller coefficient of variation, or equally consistent if the two are equal to one decimal.

Input

Two lines, each a group's name followed by its numbers, separated by spaces.

Output

Three lines.

Constraints

Each group has 2 to 1000 values and a mean greater than 0.

Example

Input:

Morning 12 14 13 15
Evening 10 20 5 25

Output:

Morning: mean 13.50, median 13.50, sd 1.29, cv 9.6%
Evening: mean 15.00, median 15.00, sd 9.13, cv 60.9%
more consistent: Morning

Why it works

The standard deviation depends on the size of the values, so it cannot compare a group around 10 with one around 1000; dividing by the mean gives a relative spread that can be compared directly.

Examples

Example 1 · Input
Morning 12 14 13 15
Evening 10 20 5 25
Expected output
Morning: mean 13.50, median 13.50, sd 1.29, cv 9.6%
Evening: mean 15.00, median 15.00, sd 9.13, cv 60.9%
more consistent: Morning
Example 2 · Input
X 2 4
Y 4 8
Expected output
X: mean 3.00, median 3.00, sd 1.41, cv 47.1%
Y: mean 6.00, median 6.00, sd 2.83, cv 47.1%
equally consistent

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

Hints

Hint 1

statistics.stdev gives the sample standard deviation; the coefficient of variation is sd ÷ mean × 100.

Hint 2

Compare the coefficients after rounding them to one decimal, as they are printed.

Solution

Show one correct solution (try it yourself first)
import statistics

groups = []
for _ in range(2):
    name, *rest = input().split()
    values = [float(x) for x in rest]
    mean = statistics.mean(values)
    sd = statistics.stdev(values)
    cv = round(sd / mean * 100, 1)
    groups.append((name, cv))
    print(f"{name}: mean {mean:.2f}, median {statistics.median(values):.2f}, sd {sd:.2f}, cv {cv:.1f}%")

(a, cva), (b, cvb) = groups
if cva == cvb:
    print("equally consistent")
else:
    print("more consistent:", a if cva < cvb else b)

Lesson: Descriptive statistics →