Help in understanding and solving a problem.


Joined
Jul 24, 2022
Messages
1
Reaction score
0
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.
IMG-3144.jpg


IMG-3145.jpg
 
Ad

Advertisements

Joined
May 11, 2022
Messages
47
Reaction score
3
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
your done
 
Last edited:
Joined
May 11, 2022
Messages
47
Reaction score
3
iin general i think it will be 2*(the number of times it switches from 0,1 or vice versa +1)
 
Joined
May 11, 2022
Messages
47
Reaction score
3
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.
your done.
 
Joined
May 11, 2022
Messages
47
Reaction score
3
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:
Ad

Advertisements

Joined
May 11, 2022
Messages
47
Reaction score
3
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
 
Joined
May 11, 2022
Messages
47
Reaction score
3
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
 
Joined
May 11, 2022
Messages
47
Reaction score
3
my recursive version needs serious work. it can't handle 7 or more inputs without going into recursion depth problems.
 
Joined
May 11, 2022
Messages
47
Reaction score
3
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
 
Ad

Advertisements

Ad

Advertisements


Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top