You have seven review slots to divide among three queues. Their weights imply shares of 1.4, 3.5, and 2.1. Rounding each share independently does not guarantee the final allocations add up to seven. Instead, give each queue its whole share first, then distribute the leftover slots in order of the largest fractional remainders.
Rank exact remainders instead of rounded decimals
Use Python 3 and save the following as allocate.py. The function accepts a nonnegative integer budget and a mapping from string queue names to nonnegative integer weights. At least one weight must be positive. Queue names break equal-remainder ties in lexicographic order, so reordering the input mapping does not change who wins the tie.
def allocate(total, weights):
if type(total) is not int or total < 0:
raise ValueError("invalid unit count")
if any(type(w) is not int or w < 0 for w in weights.values()):
raise ValueError("invalid weight")
denominator = sum(weights.values())
if denominator == 0:
raise ValueError("need positive total weight")
parts = {k: divmod(total * w, denominator) for k, w in weights.items()}
units = {k: q for k, (q, _) in parts.items()}
remaining = total - sum(units.values())
order = sorted(parts, key=lambda k: (-parts[k][1], k))
for k in order[:remaining]:
units[k] += 1
return units
weights = {"audit": 2, "search": 5, "support": 3}
got = allocate(7, weights)
assert got == {"audit": 1, "search": 4, "support": 2}
assert sum(got.values()) == 7
assert allocate(1, {"z": 1, "a": 1, "x": 0}) == {"z": 0, "a": 1, "x": 0}
assert allocate(0, weights) == dict.fromkeys(weights, 0)
try:
allocate(7, {"x": 0})
except ValueError:
pass
else:
raise AssertionError("zero weight accepted")
for key in sorted(got):
print(key, got[key])Run python3 allocate.py. Expected output:
audit 1
search 4
support 2Trace the final slot
The denominator is ten. Dividing 14, 35, and 21 by ten yields whole shares of 1, 3, and 2, with integer remainders of 4, 5, and 1. That leaves one slot for search. The denominator is shared, so comparing those integer remainders is equivalent to comparing fractional shares, without floating-point tie errors. A zero-weight queue cannot win a slot. Its share is zero, and whenever there are leftovers there are enough positive remainders to award them.
Troubleshoot policy disagreements
If all weights are zero, the proportions are undefined. Decide separately whether to postpone the allocation or substitute an explicit fallback set of weights. If the same queue repeatedly wins exact ties, a stable alphabetical policy may be inappropriate. Use a documented rotation if fairness over time matters. This method also does not handle queue caps or guarantee that each queue's allocation is monotonic when the total changes. If those are requirements, test policy scenarios or choose a different allocator before deploying. The example adapts the historical Hamilton/Vinton allocation rule to review slots, not the current method for allocating U.S. congressional seats.
Primary reference and related tutorials: U.S. Census Bureau apportionment methods · Java Samples data-science tutorials.