avatar Bite 63. Use an infinite iterator to simulate a traffic light

Complete traffic_light using itertools.cycle to return an infinite iterator to simulate a traffic light.

When called it should return a State namedtuple for each new state, here is how an islice looks:

from pprint import pprint as pp
pp(list(islice(traffic_light(), 10)))

This captures the first 10 iterated values of traffic_light into a list and pretty prints them:

[State(color='red', command='Stop', timeout=2),
 State(color='green', command='Go', timeout=2),
 State(color='amber', command='Caution', timeout=0.5),
 State(color='red', command='Stop', timeout=2),
 State(color='green', command='Go', timeout=2),
 State(color='amber', command='Caution', timeout=0.5),
 State(color='red', command='Stop', timeout=2),
 State(color='green', command='Go', timeout=2),
 State(color='amber', command='Caution', timeout=0.5),
 State(color='red', command='Stop', timeout=2)]

Good luck and have fun!

Login and get coding
go back Advanced level
Bitecoin 4X

Will you be the 221st person to crack this Bite?
Resolution time: ~21 min. (avg. submissions of 5-240 min.)
Our community rates this Bite 2.27 on a 1-10 difficulty scale.
» You can do it! 😌

Focus on this Bite hiding sidebars, turn on Focus Mode.

Ask for Help