Drill

ProblemsPython › finance

Split a total into installments

hardfinancePython

A checkout spreads a total across installments, keeping the early ones a cent higher so each slice is as equal as possible.

installment_plan(total_minor: int, installments: int) → list<int>

Solve it in the editor →

Where you start

def installment_plan(total_minor: int, installments: int) -> list[int]:
    

Worked examples

CallResult
installment_plan(100, 3)[34, 33, 33]
installment_plan(100, 6)[17, 17, 17, 17, 16, 16]
installment_plan(7, 3)[3, 2, 2]
installment_plan(10, 2)[5, 5]

Hint

Compute the floor share and the leftover, then hand the leftovers to the front.

Reference solution in Python
def installment_plan(total_minor: int, installments: int) -> list[int]:
    if installments <= 0:
        return []
    per = total_minor // installments
    extra = total_minor % installments
    return [per + 1 if i < extra else per for i in range(installments)]

The same problem in another language

More finance problems in Python