0%

The Standard Library Exists · practice

Summarize with statistics

The briefing now knows when activities happened and how many belong to each category. Its third job is to describe the session lengths.

“Average” is too vague for an API choice. We want two named summaries:

  • the mean adds the values and divides by how many there are;

  • the median is the middle after ordering, or the mean of the two middle values when the count is even.

The statistics provides both definitions:

Try it

The long 80-minute session pulls the mean upward. The median answers a different question and stays between the two middle sessions. Neither result is universally “the real average”; the report names both so its reader can see the difference.

Decide the empty case at the project boundary

The documentation says mean([]) and median([]) raise StatisticsError. That is a sensible general-library contract: no universal number represents the center of no data.

For our project, summarize_minutes([]) will return None. This makes “there is no summary” an explicit value that build_briefing can handle. Notice the division of responsibility:

  • statistics owns the mathematical operations;

  • our owns what an empty activity means to this project.

Do not catch every around the library calls. A record missing its minutes key or containing unsuitable data is a different defect, and hiding it would make the later error-handling job harder.

Why does the project return None before calling mean or median for an empty record list?

Add the summary function to the same file. Try it on both an odd and an even number of sessions, since the median is defined differently for each, and on an empty list to see your None come back.

Task

Implement summarize_minutes(records) in activity_report.py.

  • Return None immediately when records is empty.

  • Otherwise collect each record’s minutes .

  • Return a with exactly the keys mean and median.

  • Calculate those values with statistics.mean and statistics.median.

Keep the values returned by the library . Do not round them or turn them into display text; formatting belongs to the final briefing.