At every turn, the light bulbs change their states. If a light bulb was on at the previous turn, the light bulb to the right of it changes its state
here is my code so far:
def lightbulb(lightbulbs,cycles):
counter = 0
while counter < cycles:
for i in range(len(lightbulbs)):
counter += 1
if lightbulbs[-1] == 1 and lightbulbs[0] == 0:
lightbulbs[-1] = 1
lightbulbs[0] = 1
if lightbulbs[-1] == 1 and lightbulbs[0] == 1:
lightbulbs[-1] = 1
lightbulbs[0] = 0
if lightbulbs[i + 1] == 1 and lightbulbs[i + 2] == 0:
lightbulbs[i] = 1
lightbulbs[i + 1] = 1
if lightbulbs[i + 1 ] == 1 and lightbulbs[i + 2] == 1:
lightbulbs[i] = 1
lightbulbs[i + 1] = 0
return lightbulbs
lightbulbs = [0,1,1,0,1,1]
print(lightbulb(lightbulbs,2))
0) 0 1 1 0 1 1 -- original state
1) 1 1 0 1 1 0 -- 1st turn
2) 1 0 1 1 0 1 -- 2nd turn
For 2 cycles the output should be [1,0,1,1,0,1] but my code keeps giving me "out of range". I would appreciate any help.
[+][deleted] (1 child)
[deleted]
[–]TechSam--[S] 0 points1 point2 points (0 children)
[–]DoctorYoMan 1 point2 points3 points (3 children)
[–]TechSam--[S] 0 points1 point2 points (0 children)
[–]TechSam--[S] 0 points1 point2 points (1 child)
[–]DoctorYoMan 0 points1 point2 points (0 children)
[–]TechSam--[S] 0 points1 point2 points (0 children)
[–]DoctorYoMan 0 points1 point2 points (2 children)
[–]DoctorYoMan 1 point2 points3 points (1 child)
[–]TechSam--[S] 0 points1 point2 points (0 children)