A town's library service logs returned books by branch and by how late they came back. Each month's export is a list of (branch, band, count) records, and several months have been stacked together, so the same branch and band can turn up in more than one record.
The lateness bands have a natural order, best first, for example:
For each branch, the manager wants a running share: the percentage of that branch's returns that came back no later than each band. For a branch with 10 returns (6 on time, 3 that were 1–3 days late, none 4–7 days late and 1 that was 8+ days late) that is
1[60.0, 90.0, 90.0, 100.0]
since were on time, were at most 3 days late, still were at most 7 days late, and by the last band every book is counted.
Task: write running_shares(records, bands) in plain Python (no pandas needed).
records is a list of (branch, band, count) tuples, in no particular order. Counts for the same branch and band add up.bands lists every band, best first. Every record's band is in it. A band with no returns at a branch counts as 0 there.(branch, shares) tuples, one per branch that appears in records, sorted alphabetically by branch. shares holds one percentage per band, in the order of bands, each a float rounded to 4 decimal places.