Traffic Operation
Source: Dec 2024, Task 1 (DTU 02002/02003). Study solution — not an official answer key.
Problem
Traffic light mode by hour (0–23):
'rush hour'— hours 7–8 and 15–16'night'— hours 22–23 and 0–5'normal'— all other hours
Write traffic_operation(hour) returning one of those strings.
Example
>>> traffic_operation(8)
'rush hour'
Approach
Check rush-hour hours first, then night, otherwise normal.
Solution
Show solution Hide solution
Solution
def traffic_operation(hour):
if hour in (7, 8, 15, 16):
return "rush hour"
if hour >= 22 or hour <= 5:
return "night"
return "normal"