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, andsame: 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": (46, 49), "water": (183, 301)}
b = {"screen": (262, 298), "water": (28, 52)}
Output: (2, 0, 0, "B")
Explanation: screens 94% against 88%, water damage 61% against 54%: A is better at both.
Overall A fixed 229 of 350 (65%) and B 290 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 keys0 <= 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, and two that differ are different even when they round to the same float, such as 999999999 of 1000000000 and 999999998 of 999999999, which one test uses for the overall rates)
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