How many openings in the coming weeks
A recurring weekly pattern marks which of the seven weekdays are open. Count the total openings over a number of full weeks.
- The pattern list has exactly 7 booleans, one per weekday starting Monday.
- Each true entry is one opening per week.
- Multiply the weekly count by the number of weeks.
- Zero weeks gives zero openings.
WeeklyOpenings(pattern: list<bool>, weeks: 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.
Where you start
public int WeeklyOpenings(List<bool> pattern, int weeks) {
}
Worked examples
| Call | Result |
|---|---|
WeeklyOpenings(new List<bool> { true, false, true, false, true, false, false }, 4) | 12 |
WeeklyOpenings(new List<bool> { false, false, false, false, false, false, false }, 10) | 0 |
WeeklyOpenings(new List<bool> { true, true, true, true, true, true, true }, 2) | 14 |
WeeklyOpenings(new List<bool> { true, false, false, false, false, false, false }, 0) | 0 |
Hint
Count the true entries in the pattern once, then multiply.
Reference solution in C#
public int WeeklyOpenings(List<bool> pattern, int weeks) {
int perWeek = 0;
foreach (bool d in pattern) if (d) perWeek++;
return perWeek * weeks;
}