Problem 111672 · medium · Level 01 Prerequisites & Setup

Better in Every Group, Worse Overall

proportions · reading a table · combining groups · Simpson's paradox

Two repair shops, A and B, publish how many phone repairs succeeded, split by the kind of fault. Each shop's table is a dictionary from fault type to a pair (fixed, attempted). Both tables list the same fault types.

Write compare_shops(a, b) that returns a tuple (a_better, b_better, same, overall):

  • a_better: the number of fault types where A's success rate (fixed / attempted) is strictly higher than B's,
  • b_better: the number where B's is strictly higher, and same: the number where they are equal,
  • overall: "A", "B" or "tie", comparing the success rates over all repairs of each shop together.

A fault type that either shop never attempted (attempted == 0) is not compared and counts in none of the first three numbers, but its repairs (none for that shop) still belong to the totals.

Examples

Input:  a = {"screen": (81, 87), "water": (192, 263)}
        b = {"screen": (234, 270), "water": (55, 80)}
Output: (2, 0, 0, "B")
Explanation: screens 93% against 87%, water damage 73% against 69%: A is better at both.
Overall A fixed 273 of 350 (78%) and B 289 of 350 (83%), because B mostly took on the easy screen jobs.

Input:  a = {"battery": (9, 10), "screen": (4, 8)}
        b = {"battery": (18, 20), "screen": (1, 4)}
Output: (1, 0, 1, "B")

Constraints

  • 1 <= len(a) == len(b) <= 1000, with the same keys
  • 0 <= fixed <= attempted <= 10**6, and each shop attempted at least one repair overall
  • rates must be compared exactly (two rates that are equal as fractions count as the same)

Goals

  • Compare success rates group by group and overall
  • Combine groups by adding counts, never by adding percentages
  • Compare two fractions exactly with whole-number arithmetic
Starting Python…