Traffic lights are signaling devices positioned at road intersections, pedestrian crossings, and other locations to control flows of traffic. Traffic lights alternate the right way accorded to users by displaying lights of a standard color (red, orange and green) following a universal color code.

traffic light

In the typical sequence of color phases:

Assignment

Define a class TrafficLight that can be used to represent traffic lights. At any point in time, each traffic light is in one of three possible states: red, orange or green.

When creating a new traffic light (TrafficLight), the initial state of the traffic light (str; default value: red) can be passed. The only possible states are red, orange and green (lowercase).

If a traffic light $$t$$ (TrafficLight) is passed to the built-in function str, the current state (str) of traffic light $$t$$ must be returned.

If a traffic light $$t$$ (TrafficLight) is passed to the built-in function repr, a string representation (str) of traffic light $$t$$ must be returned that reads as a Python expression to create a new traffic light (TrafficLight) whose initial state is set to the current state of traffic light $$t$$.

It must be possible to call method next on a traffic light $$t$$ (TrafficLight). The method takes no arguments and always returns the value None, but puts traffic light $$t$$ in its next state: after red comes green, after green comes orange, and after orange comes red.

Example

>>> light1 = TrafficLight()
>>> light1
TrafficLight('red')
>>> light2 = TrafficLight('green')
>>> print(light1, light2)
red green
>>> light1.next()
>>> print(light1, light2)
green green
>>> light1.next()
>>> light1.next()
>>> light2.next()
>>> print(light1, light2)
red orange