Recover Total Weight From All Pairwise Crate Readings
Math & Bit ManipulationAuto-graded · 16 test casesLast updated
Tests the double-counting insight that each item appears in a fixed number of pair sums, so the total equals the sum of the readings divided by a constant.
A warehouse worker has exactly 5 crates. Every possible pair of crates is placed on a scale once, which produces 10 readings in total. The readings are given as a list, and you do not know which reading belongs to which pair of crates.
Write a function that returns the combined weight of all 5 crates.
Details
- Each of the 10 distinct pairs of crates is weighed exactly once.
- The scale is exact. Treat the readings as given data, and do not try to check whether five real weights could actually produce them.
- Individual crate weights may be non-integers.
- The order of the readings in the list carries no information.
- Return the total as a float. For example, a result of 63.75 should be returned as
63.75.
Constraints
readingscontains exactly 10 numbers (integers or floats).- Each reading is a real number.
- Every crate appears in exactly 4 of the 10 pairs.
- No validation of the input is required.
Examples
Example 1
Input: total_crate_weight([21, 22, 23, 24, 25, 26, 27, 28, 29, 30])
Output: 63.75
Hints
Hint 1
You do not need to figure out which reading belongs to which pair. Think about what happens if you add all the readings together.
Hint 2
Pick one crate and count how many of the 10 pairs include it. Each crate is paired once with every other crate.
Hint 3
If every crate shows up in exactly 4 pairs, the sum of all readings equals 4 times the combined weight. Divide accordingly.
Worked solution
Easy Try it yourself first — then compare your approach, complexity and edge cases.
Approach
Label the crate weights a, b, c, d, e. Each crate is paired with each of the other four crates exactly once, so it contributes to exactly 4 of the 10 readings. Adding all the readings therefore counts every crate's weight four times:
sum(readings) = 4 * (a + b + c + d + e)
The total weight is the sum of the readings divided by 4.
Why it is correct
Addition is commutative, so the sum of the readings does not depend on which pair produced which reading. The only thing that matters is how many times each crate is included, and that is always 4 (the number of partners it has). Dividing the grand sum by 4 gives the exact total. Non-integer results are fine because crate weights are not required to be whole numbers. Dividing by 4 is exact in binary floating point, so there is no rounding concern for integer inputs.
For the readings 21 through 30, the sum is 255, so the total is 255 / 4 = 63.75.
Complexity
- Time: O(k) for k readings, a single pass to compute the sum (k = 10 here).
- Space: O(1) extra.
Reference solution
from typing import List
def total_crate_weight(readings: List[float]) -> float:
# Each crate appears in exactly 4 of the 10 pairwise readings,
# so the sum of all readings equals 4 * (total weight).
return sum(readings) / 4