Drill

ProblemsPython › dates

Count the working days in a span

mediumdatesPython

An SLA clock only runs on working days, so a span of calendar days has to be reduced to the ones that count.

business_days(start_weekday: int, span_days: int) → int

Solve it in the editor →

Where you start

def business_days(start_weekday: int, span_days: int) -> int:
    

Worked examples

CallResult
business_days(0, 7)5
business_days(0, 5)5
business_days(5, 2)0
business_days(4, 4)2

Hint

Walk the days and take the weekday modulo 7, or work out the whole weeks first and handle the remainder.

Reference solution in Python
def business_days(start_weekday: int, span_days: int) -> int:
    if span_days <= 0 or start_weekday < 0 or start_weekday > 6:
        return 0
    return sum(1 for i in range(span_days) if (start_weekday + i) % 7 < 5)

The same problem in another language

More dates problems in Python