The True Middle

Python FundamentalsHard+10 XP+10

The True Middle

An average can be dragged around by one wild value. The median is the middle value once everything is in order, and it barely notices outliers.

Task: write median(values) that returns the median of a list of numbers.

  • Put the values in order, smallest first.
  • If there's an odd number of values, the median is the one in the middle.
  • If there's an even number, there are two middle values. The median is their average (add them, divide by 2).
  • values always has at least one number.

Examples

values = [7, 1, 5]
returns 5
values = [4, 1, 3, 2]
returns 2.5
values = [10]
returns 10

Hints

Solve it to unlock the solution

Once your code passes every test, a reference solution shows up here so you can compare it with yours.

solution.py
CtrlEnterto run