Editor

Split the Bill

#253 · Python Basics · Beginner · 40 XP · about 8 min

A group finishes dinner and wants to split the bill evenly, tip included. Write split_bill(total_cents, tip_percent, people) that returns a list with the amount each person pays, in cents.

Work it out like this:

  • The tip is tip_percent percent of total_cents, rounded down to a whole cent.
  • The total plus the tip is shared equally between people people.
  • If the cents do not divide evenly, the leftover cents go one each to the first people in the list, so nobody pays more than one cent extra.

The list has one amount per person, and the amounts add up to the total plus the tip.

Examples

split_bill(10000, 15, 3)  # returns [3834, 3833, 3833]
split_bill(2599, 10, 2)   # returns [1429, 1429]
split_bill(999, 20, 4)    # returns [300, 300, 299, 299]

In the first example the tip is 1500 cents, so 11500 cents are shared by 3 people: 3833 each with 1 cent left over, which the first person pays. In the second, 10% of 2599 is 259.9, rounded down to 259.

Constraints

  • 0 <= total_cents <= 1000000
  • 0 <= tip_percent <= 100, a whole number
  • 1 <= people <= 50

Fastest solvers

Leaderboard

First solves only, ranked by stars and then time.