Problem 136891 · easy · Level 01 Prerequisites & Setup

Merging the Branch Averages

weighted mean · mean · tuples · loops

A library has several branches. Each branch reports only two numbers about last month: the mean number of books its members borrowed and the number of members. Head office wants the mean number of books per member over the whole library.

Write library_mean(branches) where branches is a list of (branch_mean, members) pairs, and return the mean over all members of all branches as a float. A branch with 0 members reports None as its mean and adds nobody. If the library has no members at all, return None.

Examples

Input:  branches = [(4.0, 30), (10.0, 10)]
Output: 5.5
Explanation: 30 members borrowed 120 books and 10 members borrowed 100,
so 220 books over 40 members is 5.5 each (not 7.0, the mean of the two means).

Input:  branches = [(3.5, 12), (None, 0), (2.0, 4)]
Output: 3.125

Input:  branches = [(None, 0)]
Output: None

Constraints

  • 0 <= len(branches) <= 10**4
  • 0 <= members <= 10**5; a branch mean is None exactly when its members is 0, and otherwise a number with 0 <= branch_mean <= 1000

Goals

  • Combine group means into the mean of everybody
  • Recover each group's total from its mean and its size
  • Decide what an empty group contributes
Starting Python…