Drill

ProblemsPython › billing

Clamp a day into a billing term

easybillingPython

A billing window runs from day 1 to day termDays. Clamp any given day to within that window.

clamp_to_term(term_days: int, day: int) → int

Solve it in the editor →

Where you start

def clamp_to_term(term_days: int, day: int) -> int:
    

Worked examples

CallResult
clamp_to_term(31, 35)31
clamp_to_term(31, 0)1
clamp_to_term(31, 20)20
clamp_to_term(0, 5)0

Hint

A pair of comparisons: too low pulls up, too high pulls down.

Reference solution in Python
def clamp_to_term(term_days: int, day: int) -> int:
    if term_days <= 0:
        return 0
    if day < 1:
        return 1
    if day > term_days:
        return term_days
    return day

The same problem in another language

More billing problems in Python