Problems › Python › scheduling
Rest-period violations in the hand-offs
Between any two shifts assigned to the same person there must be at least restMinutes of free time. Count the hand-offs in a day that break the rule.
- Each pair of consecutive shifts is one hand-off; consider them in the order listed.
- Sequential per person — here the day is one person's list, so every gap counts.
- Ending at 900 and starting at 1020 is exactly restMinutes if restMinutes is 120, which is fine.
rest_violations(shifts: list<Shift>, rest_minutes: int) → int
Where you start
def rest_violations(shifts: list[Shift], rest_minutes: int) -> int:
Worked examples
| Call | Result |
|---|---|
rest_violations([Shift(start=480, end=900), Shift(start=960, end=1200)], 120) | 1 |
rest_violations([Shift(start=480, end=900), Shift(start=1020, end=1200)], 120) | 0 |
rest_violations([Shift(start=0, end=100)], 100) | 0 |
rest_violations([Shift(start=0, end=300), Shift(start=400, end=500), Shift(start=700, end=800)], 300) | 2 |
Hint
Compare each next start to the previous end.
Reference solution in Python
def rest_violations(shifts: list[Shift], rest_minutes: int) -> int:
return sum(1 for i in range(1, len(shifts)) if shifts[i].start - shifts[i - 1].end < rest_minutes)