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):

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"