A quiz night splits its prize money between the winning teams. Write prize_shares(total_cents, teams) that returns a list of teams shares in cents: the shares add up to exactly total_cents and differ by at most one cent, larger shares first.
The function is used by other programs, so it must refuse nonsense loudly instead of returning something made up:
- If
total_centsorteamsis not anint(afloat, a string, or aboolsuch asTrue), raiseTypeError. - If
teamsis less than 1, ortotal_centsis negative, raiseValueError.
The tests check errors with the helper raises(fn, *args), which returns the name of the exception that fn(*args) raises, or None.
Examples
Input: prize_shares(1000, 3)
Output: [334, 333, 333]
Input: raises(prize_shares, 1000, 0)
Output: "ValueError"
Input: raises(prize_shares, 10.5, 2)
Output: "TypeError"
Constraints
teamscan be up to10**5;total_centsup to10**12.- The type checks come before the value checks:
prize_shares(-5.0, 2)raisesTypeError.
Goals
- Raise `ValueError` for a value that makes no sense and `TypeError` for a value of the wrong kind
- Check the arguments before doing any work
- Split an amount of cents fairly with `divmod`