Pybites Logo

Calculate the median from a dictionary

Level: Intermediate (score: 3)

You are in charge of a program that continuously collects measurements. On demand, it outputs the median of the collected numbers.

In order to save memory, the numbers are not saved in a list but in a dictionary containing the counts of each number, similar to collections.Counter.

Write a function that calculates the median from the supplied dictionary.

Example:

>>> d = {1: 3, 2: 1, 4: 3}
# 1, 1, 1, 2, 4, 4, 4
>>> calc_median_from_dict(d)
2