There are N switches (from 1 to N) and also N bulbs (from 1 to N).
the switch is can be pressed only once and it can turn ON/OFF the bulb. a switch can turn ON/OFF more than 1 bulbs.
For example:
The switch number 12 can turn ON/OFF the bulbs {1, 2, 3, 4, 6, 12}
(because {1, 2, 3, 4, 6, 12}
are the divisors of 12)
So we have a function main(N,setON)
- N is the number of lights
- SetOn is set of bulbs that are from the beginning are ON
The function must return list of switches that are served to turn ON all the bulbs
For example given, N=6
and setON = {2,4}
, return the list [2,5,6]
- after pressed the button 2 will light on
{1,4}
bulbs - after pressed the button 5 will light on
{4,5}
bulbs - after pressed the button 6 will light on
{1,2,3,4,5,6}
bulbs
I started this question but I'm out of ideas how to solve this. I did it in way that I call a function divisors that finds for each number the divisors and after it returns a list of divisors of number N and then I run a loop for each list to see where it turns ON or OFF and put the number of the switch to a new list. But I actually don't get the result that I want, and I'm out of ideas.
I would like to here your suggestions, I'm not requesting of you solve my problem, just give me a hint :) or if you want correct me.
import math
def main(N, setON):
newList= []
acces = set(range(1,N+1))
turnOFF = set()
for number in range(N,1,-1):
divs = divisors(number)
for i in divs:
if i in setON:
turnOFF.add(i)
setON.remove(i)
else:
setON.add(i)
if i in turnOFF:
turnOFF.remove(i)
newList.append(divs[-1])
if len(setON) == N-1:
print(newList)
def divisors(n):
divs = [x for x in range(1, int(math.sqrt(n))+1) if n % x == 0]
opps = [int(n/x) for x in divs]
return list(set(divs + opps))
N=6
setON={2,4}
main(N, setON)
It gives me [6, 5, 4]