Python · Intermediate · 35 min

Median values by region

Group records and handle odd and even sample sizes explicitly.

Task

Implement median_by_region(rows). Each row is a pair (region, value). Return the median value for every region without changing the input.

Requirements

  • rows must be a list of two-item tuples or lists. Region names are nonempty strings.
  • Values must be finite nonnegative int or float values; bool is invalid. Invalid input raises ValueError.
  • An even-sized group uses the average of its two middle values. A single value is its own median.
  • Return a dictionary; empty input returns an empty dictionary.
GroupingMedianValidation

Loading exercise…