Skip to the content

The tiered tribute

Problem

Every autumn the grain carts roll into the city of Ur, and the scribe collects the tribute in brackets. If the caps are 100 and 500 and the rates are 0, 10 and 20 percent, the first 100 sacks pay nothing, sacks 101 to 500 pay 10%, and every sack past 500 pays 20%. Each bracket pays only for the sacks that fall in it.

Write a function that takes limits (the caps, positive integers from smallest to largest), rates (whole percentages, always one more than the caps: the last one is for the bracket with no cap) and harvest (sacks, an integer from 0 up), and returns the tribute in sacks as a number. It can have decimals: don't round it. limits can be empty: then the whole harvest pays the only rate.

With 800 sacks: 100 × 0% + 400 × 10% + 300 × 20% = 0 + 40 + 60 = 100.

Examples

  • Reaches the last bracket

    [100, 500], [0, 10, 20], 800 → 100

  • Stops in the middle bracket

    [100, 500], [0, 10, 20], 300 → 20

  • Never leaves the first bracket

    [100, 500], [0, 10, 20], 80 → 0

  • Right on a cap

    [100, 500], [0, 10, 20], 500 → 40

Besides these, the challenge has hidden tests that are revealed when you submit your solution.

You start with this

Python

def tax(limits, rates, harvest):
    pass

JavaScript

function tax(limits, rates, harvest) {
}
Solve this challenge

It opens in your browser, with the editor and the tests. This challenge is part of the full plan; the O(1) and O(log n) ones are free.

More O(n) challenges

See all challenges →