Drill

ProblemsC# › events

How many active weekdays in a span

mediumeventsArraysMathC#

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

ActiveDayCount(activeDays: list<int>, days: 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 ActiveDayCount(List<int> activeDays, int days) {
    
}

Worked examples

CallResult
ActiveDayCount(new List<int> { 0 }, 7)1
ActiveDayCount(new List<int> { 0, 2, 4 }, 7)3
ActiveDayCount(new List<int> { 0, 2, 4 }, 10)5
ActiveDayCount(new List<int> { 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 C#
public int ActiveDayCount(List<int> activeDays, int days) {
    bool[] active = new bool[7];
    foreach (int d in activeDays) active[d] = true;
    int count = 0;
    for (int i = 0; i < days; i++) if (active[i % 7]) count++;
    return count;
}

The same problem in another language

More events problems in C#