Help in understanding and solving a problem.

oliver69

I got this problem in my test, and couldn't understand the problem. Please help me out in understanding it. The sample test case given, I tried to trace it out but am still not sure how it's done.

phillip1882

your right an output of 13 makes no sense.
i1. switch the middle bulb
2. swith the right bulb
3. switch the left bulb
4. switch the rightbulb
5 swith the middle bulb
6 switch the right bulb

Last edited:

phillip1882

iin general i think it will be 2*(the number of times it switches from 0,1 or vice versa +1)

phillip1882

Oh shoot I missed that extra 1. Its not 101, it 1011

phillip1882

so yeah
1. switch the right most bulb
2. switch the second bulb
3. switch the right most bulb
4. switch the third bulb
5. switch the right most bulb
6. switch the first bulb
7. switch the right most bulb
8 switch the third bulb
9. switch the right most bulb
10 switch the second bulb.
11.sitch the right most bulb
12. switch the third bulb
13. switch the right most bulb.

phillip1882

Python:
``````import sys
def bulb(lights,i,steps):
print(lights)
if i == len(lights):
i = 0
while lights[i] =="0":
i += 1
if i == len(lights):
print(steps)
input()
sys.exit(0)
if i == len(lights)-2:
if lights[i+1] == "1":
if lights[i] == "0":
lights = lights[:i]+"1"+lights[i+1:]
else:
lights = lights[:i]+"0"+lights[i+1:]
steps += 1
i += 1
bulb(lights,i,steps)

elif i < len(lights)-2 and lights[i+1] == "1" and "1" not in lights[i+2:]:
if lights[i] == "0":
lights = lights[:i]+"1"+lights[i+1:]
else:
lights = lights[:i]+"0"+lights[i+1:]
i+= 1
steps += 1
bulb(lights,i,steps)
if steps %2 == 0:
if lights[-1] == "0":
lights = lights[:-1]+"1"
else:
lights = lights[:-1]+"0"
steps += 1
bulb(lights,i,steps)
else:
i += 1
bulb(lights,i,steps)

bulb("10110",0,0))``````

Last edited:

phillip1882

here's my linear version
Python:
``````i = 0
steps = 0
light = input("code? ")
check = True
while check:
print(light)
if i >= len(light):
i = 0
while light[i] =="0":
i += 1
if i == len(light):
check = False
print(steps)
break
if steps%2 == 0:
if light[-1] == "0":
light = light[:-1] +"1"
else:
light = light[:-1] +"0"
i += 1
steps += 1
elif i == len(light)-2:
if light[i] == "0":
light = light[:i] +"1"+light[i+1:]
else:
light = light[:i] +"0"+light[i+1:]
i+= 1
steps += 1
elif i < len(light)-2 and light[i+1] =="1" and "1" not in light[i+2:]:
if light[i] == "0":
light = light[:i] +"1"+light[i+1:]
else:
light = light[:i] +"0"+light[i+1:]
i += 1
steps += 1
else:
i+=1``````

phillip1882

corrected an error:
Python:
``````i = 0
steps = 0
light = "100101111100"
check = True
while check:
print(light)
if i >= len(light):
i = 0
while light[i] =="0":
i += 1
if i == len(light):
check = False
print(steps)
break
if i == len(light)-2 and light[-1] == "1":
if light[i] == "0":
light = light[:i] +"1"+light[i+1:]
else:
light = light[:i] +"0"+light[i+1:]
i+= 1
steps += 1
elif i < len(light)-2 and light[i+1] =="1" and "1" not in light[i+2:]:
if light[i] == "0":
light = light[:i] +"1"+light[i+1:]
else:
light = light[:i] +"0"+light[i+1:]
i += 1
steps += 1
elif len(light)-1 == i:
if light[-1] == "0":
light = light[:-1] +"1"
else:
light = light[:-1] +"0"
i += 1
steps += 1
else:
i+=1``````

phillip1882

my recursive version needs serious work. it can't handle 7 or more inputs without going into recursion depth problems.

phillip1882

Python:
``````i = 0
steps = 0
light = "101110"
id = -2
while light[id] =="1":
id -= 1
if id%2 == 0:
if light[-1] =="0":
steps += 1
light = light[:-1]+"1"
else:
if light[-1] == "1":
steps+= 1
light = light[:-1] +"0"
check = True
while check:
print(light)
if i >= len(light):
i = 0
while light[i] =="0":
i += 1
if i == len(light):
check = False
print(steps)
break
if i == len(light)-2 and light[-1] == "1":
if light[i] == "0":
light = light[:i] +"1"+light[i+1:]
else:
light = light[:i] +"0"+light[i+1:]
i+= 1
steps += 1
elif i < len(light)-2 and light[i+1] =="1" and "1" not in light[i+2:]:
if light[i] == "0":
light = light[:i] +"1"+light[i+1:]
else:
light = light[:i] +"0"+light[i+1:]
i += 1
steps += 1
elif len(light)-1 == i:
if light[-1] == "0":
light = light[:-1] +"1"
else:
light = light[:-1] +"0"
i += 1
steps += 1
else:
i+=1``````

phillip1882

further corrections