Traffic light simulation in Raspberry Pi - Time & Space Complexity
When simulating a traffic light, we want to know how the program's running time changes as we add more cycles or lights.
We ask: How does the time to run the simulation grow when we increase the number of light changes?
Analyze the time complexity of the following code snippet.
for cycle in range(num_cycles):
turn_on_red()
wait(red_duration)
turn_on_green()
wait(green_duration)
turn_on_yellow()
wait(yellow_duration)
# End of simulation
This code runs a traffic light sequence for a set number of cycles, turning lights on and waiting for their durations.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The for-loop that repeats the light changes for each cycle.
- How many times: Exactly
num_cyclestimes, once per cycle.
Each cycle runs the same fixed steps of turning lights on and waiting.
| Input Size (num_cycles) | Approx. Operations |
|---|---|
| 10 | About 10 cycles of light changes |
| 100 | About 100 cycles of light changes |
| 1000 | About 1000 cycles of light changes |
Pattern observation: The total work grows directly with the number of cycles; doubling cycles doubles the work.
Time Complexity: O(n)
This means the time to run the simulation grows in a straight line as you add more cycles.
[X] Wrong: "The time to run the simulation stays the same no matter how many cycles we have."
[OK] Correct: Each cycle repeats the same steps, so more cycles mean more total time.
Understanding how loops affect running time helps you explain how programs scale, a key skill in many coding tasks.
"What if we added a nested loop inside each cycle to blink the yellow light multiple times? How would the time complexity change?"
Practice
Solution
Step 1: Understand GPIO pin function
GPIO pins on Raspberry Pi are used to send signals to external devices like LEDs.Step 2: Connect GPIO pins to LEDs
In traffic light simulation, GPIO pins turn LEDs on or off to show red, yellow, or green lights.Final Answer:
To control the LEDs turning on and off -> Option AQuick Check:
GPIO pins control LEDs = A [OK]
- Thinking GPIO speeds up the CPU
- Confusing GPIO with internet connection
- Assuming GPIO stores data
Solution
Step 1: Identify correct GPIO setup function
GPIO.setup(pin_number, mode) is used to set pin mode; mode can be GPIO.OUT or GPIO.IN.Step 2: Choose correct mode for LED output
LEDs need output mode, so GPIO.OUT is correct for pin 17.Final Answer:
GPIO.setup(17, GPIO.OUT) -> Option BQuick Check:
Setup pin as output = B [OK]
- Using GPIO.output instead of GPIO.setup to set mode
- Setting pin as GPIO.IN instead of GPIO.OUT
- Confusing setup and output functions
GPIO.output(18, GPIO.HIGH) time.sleep(3) GPIO.output(18, GPIO.LOW)
Solution
Step 1: Analyze GPIO.output commands
GPIO.HIGH turns the LED on; GPIO.LOW turns it off.Step 2: Understand time.sleep effect
time.sleep(3) pauses the program for 3 seconds while LED stays on.Final Answer:
The LED on pin 18 turns on for 3 seconds, then turns off -> Option DQuick Check:
HIGH then sleep then LOW = C [OK]
- Thinking LED blinks rapidly without loop
- Assuming syntax error due to time.sleep
- Confusing HIGH with LOW states
GPIO.setup(22, GPIO.OUT) GPIO.output(22, HIGH) time.sleep(2) GPIO.output(22, LOW)
Solution
Step 1: Check usage of HIGH and LOW constants
HIGH and LOW must be written as GPIO.HIGH and GPIO.LOW to be recognized.Step 2: Verify other code parts
GPIO.setup with GPIO.OUT is correct; time.sleep works fine; pin 22 is valid.Final Answer:
HIGH and LOW should be prefixed with GPIO. -> Option AQuick Check:
HIGH/LOW require GPIO. prefix = D [OK]
- Omitting GPIO. prefix for HIGH/LOW
- Changing pin mode incorrectly
- Thinking time.sleep causes error
Solution
Step 1: Check order and timing of lights
Traffic lights cycle red (2s), yellow (1s), green (3s) in that order repeatedly.Step 2: Verify code matches timing and order
while True: GPIO.output(5, GPIO.HIGH) time.sleep(2) GPIO.output(5, GPIO.LOW) GPIO.output(6, GPIO.HIGH) time.sleep(1) GPIO.output(6, GPIO.LOW) GPIO.output(13, GPIO.HIGH) time.sleep(3) GPIO.output(13, GPIO.LOW) uses while True loop, turns on red (pin 5) for 2s, then yellow (pin 6) for 1s, then green (pin 13) for 3s, turning off each after delay.Final Answer:
while True loop with red (2s), yellow (1s), green (3s) -> Option CQuick Check:
Correct order and timing in loop = A [OK]
- Wrong order of lights
- Using for loop without timing per light
- Turning all LEDs on at once
Ready to prove it?
Pass the test and this pattern moves unknown
