Count the working days in a span
An SLA clock only runs on working days, so a span of calendar days has to be reduced to the ones that count.
- Weekdays are numbered 0 for Monday through 6 for Sunday.
- Saturday and Sunday do not count.
- The span starts on the given weekday and runs for that many consecutive days.
- A span of zero or less, or a weekday outside 0 to 6, counts nothing.
businessDays(startWeekday: int, spanDays: int) → int
Go 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
func businessDays(startWeekday int, spanDays int) int {
}
Worked examples
| Call | Result |
|---|---|
businessDays(0, 7) | 5 |
businessDays(0, 5) | 5 |
businessDays(5, 2) | 0 |
businessDays(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 Go
func businessDays(startWeekday int, spanDays int) int {
if spanDays <= 0 || startWeekday < 0 || startWeekday > 6 {
return 0
}
n := 0
for i := 0; i < spanDays; i++ {
if (startWeekday+i)%7 < 5 {
n++
}
}
return n
}