Drill

ProblemsC# › billing

Clamp a day into a billing term

easybillingMathC#

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

ClampToTerm(termDays: int, day: int) → int

C# needs a compiler and Drill does not host one yet, so this page is the reference rather than an exercise: the problem, worked examples, and the solution in full. To type it out, the same problem runs in Python.

Solve it in Python →

Where you start

public int ClampToTerm(int termDays, int day) {
    
}

Worked examples

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

Hint

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

Reference solution in C#
public int ClampToTerm(int termDays, int day) {
    if (termDays <= 0) return 0;
    if (day < 1) return 1;
    if (day > termDays) return termDays;
    return day;
}

The same problem in another language

More billing problems in C#