Drill

ProblemsPython › payments

Split a bill without losing a kuruş

mediumpaymentsPython

A table of friends splits the bill. The app has to hand out whole minor units that add back to exactly what was charged.

split_bill(total: int, people: int) → list<int>

Solve it in the editor →

Where you start

def split_bill(total: int, people: int) -> list[int]:
    

Worked examples

CallResult
split_bill(1000, 3)[334, 333, 333]
split_bill(1000, 4)[250, 250, 250, 250]
split_bill(10, 4)[3, 3, 2, 2]
split_bill(0, 3)[0, 0, 0]

Hint

Base share is total / people. The first (total % people) people pay one more.

Reference solution in Python
def split_bill(total: int, people: int) -> list[int]:
    if people <= 0 or total < 0:
        return []
    base, extra = divmod(total, people)
    return [base + 1 if i < extra else base for i in range(people)]

The same problem in another language

More payments problems in Python