Panel of Light Switches
Implement a class LightPanel for a row of n lights numbered 0 to n - 1. Every light starts off. Write these methods:
LightPanel(n)makes the panel.toggle(i)flips lighti: off becomes on and on becomes off. Returnstrueif lightiis on afterwards,falseif it is off.is_on(i)returnstrueif lightiis on.count_on()returns how many lights are on.all_off()turns every light off and returns how many lights were on just before.
i is always between 0 and n - 1. The tests call the methods in sequence and compare every return value. The expected list holds null for the constructor.
Example 1
LightPanel(3)
toggle(0) returns true
toggle(2) returns true
toggle(0) returns false
is_on(2) returns true
count_on() returns 1
all_off() returns 1
is_on(2) returns false
count_on() returns 0
Light 0 is switched on and then off again, so only light 2 is on when count_on runs. all_off reports that one light was on and switches it off.
Constraints
1 <= n <= 200 <= i < n- Each test makes
1to40method calls after the constructor.
Write it in your browser, against the clock. The model solution, stepped through line by line, comes after.
More Classes problems
- Rectangle on the TableBeginner
- Scoreboard by RoundsBeginner
- Stack With a Size LimitBeginner
- Take a NumberBeginner
- Tally CounterBeginner
- Thermostat Between Two LimitsBeginner
- Whose Turn Is ItBeginner
- Average of the Last K ReadingsEasy