How many active weekdays in a span
A project runs for a number of calendar days but only counts effort on specified weekdays. Return the number of active days.
- activeDays lists weekday numbers 0 (Monday) through 6 (Sunday).
- Calendar day 0 is the first day; day d falls on weekday d mod 7.
- Count every day in 0 .. days−1 whose weekday appears in activeDays.
- An empty activeDays list means no days count.
activeDayCount(activeDays: list<int>, days: int) → int
Java 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.
Where you start
int activeDayCount(List<Integer> activeDays, int days) {
}
Worked examples
| Call | Result |
|---|---|
activeDayCount(Main.<Integer>ls(0), 7) | 1 |
activeDayCount(Main.<Integer>ls(0, 2, 4), 7) | 3 |
activeDayCount(Main.<Integer>ls(0, 2, 4), 10) | 5 |
activeDayCount(Main.<Integer>ls(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 Java
int activeDayCount(List<Integer> activeDays, int days) {
boolean[] active = new boolean[7];
for (int d : activeDays) active[d] = true;
int count = 0;
for (int i = 0; i < days; i++) if (active[i % 7]) count++;
return count;
}