Fix the cent split for a shared charge
Your task
Your payments feature splits one charge evenly across several people. The first version did the math in floating point dollars, and splitting $100 three ways gave three shares of $33.33 that lost a cent. The new version works in whole cents.
An assistant wrote split_cents_evenly(total_cents, parties). It is in the editor. It has a bug. Find it and fix it.
Input and output:
total_cents: the charge, in cents.parties: how many people share it.- Return one share per person, in cents, as a list. Return
[]when the input is invalid.
Rules the tests check:
- The input is invalid when
partiesis 0 or less, ortotal_centsis less than 0. - The shares add up to exactly
total_cents. - Every share is
total_centsdivided byparties, rounded down, or that amount plus 1 cent. - The extra cents go to the first people in the list, one each.
Examples
split_cents_evenly(total_cents: int, parties: int) -> list[int]Example 1
Input
split_cents_evenly(10000, 4)Output
[2500, 2500, 2500, 2500]Example 2
Input
split_cents_evenly(10000, 3)Output
[3334, 3333, 3333]Example 3
Input
split_cents_evenly(10, 3)Output
[4, 3, 3]