Drill

ProblemsPython › pricing

Price an order across volume tiers

mediumpricingPython

A wholesaler charges less per unit the more you buy, and the tiers stack: the first slice of the order is charged at the first rate, the next slice at the second, and so on.

tiered_total(qty: int, tiers: list<Tier>) → int

Solve it in the editor →

Where you start

def tiered_total(qty: int, tiers: list[Tier]) -> int:
    

Worked examples

CallResult
tiered_total(60, [Tier(up_to=10, unit_price=100), Tier(up_to=50, unit_price=90), Tier(up_to=200, unit_price=80)])5400
tiered_total(5, [Tier(up_to=10, unit_price=100), Tier(up_to=50, unit_price=90)])500
tiered_total(300, [Tier(up_to=10, unit_price=100), Tier(up_to=50, unit_price=90)])27100
tiered_total(0, [Tier(up_to=10, unit_price=100)])0

Hint

Track how many units you have already priced. Each tier handles at most upTo minus that.

Reference solution in Python
def tiered_total(qty: int, tiers: list[Tier]) -> int:
    if qty <= 0 or not tiers:
        return 0
    done = 0
    total = 0
    for t in tiers:
        take = min(qty - done, t.up_to - done)
        if take > 0:
            total += take * t.unit_price
            done += take
    if done < qty:
        total += (qty - done) * tiers[-1].unit_price
    return total

The same problem in another language

More pricing problems in Python