Problem 329456 · easy · Level 03 Linear Management & Searching

Sharing the Prize Money, or Refusing To

py-exceptions · raise · ValueError · TypeError · divmod

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_cents or teams is not an int (a float, a string, or a bool such as True), raise TypeError.
  • If teams is less than 1, or total_cents is negative, raise ValueError.

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

  • teams can be up to 10**5; total_cents up to 10**12.
  • The type checks come before the value checks: prize_shares(-5.0, 2) raises TypeError.

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`
Starting Python…