Drill

ProblemsPython › events

How many active weekdays in a span

mediumeventsPython

A project runs for a number of calendar days but only counts effort on specified weekdays. Return the number of active days.

active_day_count(active_days: list<int>, days: int) → int

Solve it in the editor →

Where you start

def active_day_count(active_days: list[int], days: int) -> int:
    

Worked examples

CallResult
active_day_count([0], 7)1
active_day_count([0, 2, 4], 7)3
active_day_count([0, 2, 4], 10)5
active_day_count([3], 1)0

Hint

Build a 7-slot lookup array of booleans once, then loop over the days and test each weekday against it.

Reference solution in Python
def active_day_count(active_days: list[int], days: int) -> int:
    active = [False] * 7
    for d in active_days:
        active[d] = True
    count = 0
    for i in range(days):
        if active[i % 7]:
            count += 1
    return count

The same problem in another language

More events problems in Python