Problem 173459 · hard · Level 01 Prerequisites & Setup

A Hundred Squares for the Poster

percentages · rounding · remainders · integer arithmetic · ties

A poster shows survey results as a grid of 100 squares, each square coloured by an answer, so every answer needs a whole number of squares and the numbers must add up to exactly 100. Plain rounding does not guarantee that: three equal answers round to 33 + 33 + 33 = 99.

The designer uses this rule. Let N be the number of responses. Each answer with count c first gets 100 * c / N squares rounded down. The squares still missing are then handed out one each to the answers whose exact share lost the most in that rounding down (the largest leftover fraction). If two answers lost exactly the same amount, the one that comes earlier in the list goes first.

Write poster_squares(counts) that returns the list of square counts, in the same order as counts.

Examples

Input:  counts = [1, 1, 1]
Output: [34, 33, 33]
Explanation: each exact share is 33.33...; rounding down gives 99, and the three leftovers
are equal, so the first answer gets the missing square.

Input:  counts = [13, 7, 5]
Output: [52, 28, 20]
Explanation: the exact shares are 52, 28 and 20, nothing is missing.

Input:  counts = [11, 20, 9]
Output: [28, 50, 22]
Explanation: the shares are 27.5, 50 and 22.5; rounding down gives 99, and the leftovers
0.5, 0 and 0.5 tie between the first and last answers, so the first one wins the square.

Constraints

  • 1 <= len(counts) <= 10**5, every count a whole number with 0 <= count <= 10**9, and at least one count is positive
  • leftovers must be compared exactly: two leftovers that are equal as fractions are a tie, even if floating-point arithmetic would make them differ in the last digit

Goals

  • See why rounded percentages need not add up to 100
  • Share out whole units fairly by exact remainders
  • Break ties with a fixed rule using whole-number arithmetic only
Starting Python…