Predict the output
A 10.00 EUR refund is split across 3 sellers in integer cents. What prints?
total = 1000 # cents
shares = [total // 3] * 3
shares[0] += total - sum(shares)
print(shares, sum(shares))Hint 1 of 1
What is 1000 // 3, and what is left over?
Choose an answer, then open Show answer below to check it.
Text version
Boxes on the board, in order:
- Client
- Payments API
- Postgres + outbox
- Worker
- Provider
- Webhook handler
Show answer
Answer: A [334, 333, 333] 1000
Correct. Floor division gives 333 each, and the leftover cent goes to the first share, so nothing is lost.
Integer cents can't be split three ways evenly, so someone must get the extra cent. Floor division gives 333 each, the 1 cent left over goes to the first share, and the total stays 1,000.
Why not the others?
- B
[333, 333, 333] 999. The third line adds the missing cent back before printing. - C
[333.33, 333.33, 333.33] 999.99.//on integers gives integers; there are no fractions of a cent here. - D
[334, 334, 334] 1002. Only the first share gets the remainder, and the remainder is 1.