Mean, median and mode

Describe a list of whole numbers.

Input

One line: 1 to 1000 whole numbers, separated by spaces.

Output

Three lines: mean M, median D and mode O, with the mean and median to 2 decimal places. If several values share the highest count, the mode is the smallest of them.

Examples

Example 1 · Input
62 70 70 75 81 88 91
Expected output
mean 76.71
median 75.00
mode 70
Example 2 · Input
4 1 3 2
Expected output
mean 2.50
median 2.50
mode 1

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

Hints

Hint 1

Sort a copy of the list for the median.

Hint 2

For an even count, the median is the average of the two middle values.

Hint 3

The mode is the value that appears most; ties go to the smallest.

Solution

Show one correct solution (try it yourself first)
nums = sorted(int(x) for x in input().split())
n = len(nums)
mid = n // 2
median = nums[mid] if n % 2 else (nums[mid - 1] + nums[mid]) / 2
best = max(nums.count(v) for v in set(nums))
mode = min(v for v in set(nums) if nums.count(v) == best)
print(f"mean {sum(nums) / n:.2f}")
print(f"median {median:.2f}")
print("mode", mode)

Lesson: Describing data →