### Thread: Help with selectively toggling boolean elements in an array, please!

1. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
Nov 2013
Posts
2
Rep Power
0

#### Help with selectively toggling boolean elements in an array, please!

I have a string of 50 Christmas lights in a single line, and when they are first plugged in, all of the lights are off. The lights are controlled by a single button, where every time the button is pressed, some of the lights flip their state (i.e. if they are off, they change to on; if they are on, they change to off). The selection of lights which change depends on how many times the button has been pressed so far:

On the 1st press, the 1st, 2nd, 3rd, 4th, …, 50th lights change state
On the 2nd press, the 2nd, 4th, 6th, …, lights change state;
On the 3rd press, the 3rd, 6th, 9th, …, lights change state;
etc.
On the nth press, the lights corresponding to all multiples of n change state.
How do I find out what lights are on after any given number of "n" presses?

I tried defining a boolean array to represent the lights. I wrote the following function:

Code:
```def pressbutton(i):

lightsarray = [False] * 50 ### All lights off at start ***
for count in range(1, i+1):
lightsarray[::count] = [not y for y in lightsarray[::count]]
return lightsarray```
Keeping in mind that False = OFF and True = ON, let's look at the first 5 lights. This is what is expected:

For 0 presses, everything is off:

Code:
`False, False, False, False, False...`
For 1 press, lights at every multiple of 1 are flipped:
Code:
`True, True, True, True, True...`
For 2 presses, the lights at every multiple of 2 are flipped:
Code:
`True, False, True, False, True...`
Note that the second and the fourth light were toggled, otherwise the rest remained unchanged.
For 3 presses, the lights at every multiple of 3 are flipped:
Code:
`True, False, False, False, True...`
However I see that for instance, 2 presses gives False, True... not True, False... and 3 presses gives True, True, False, False, False... (there are two Trues in the beginning.)

How do I correct this? I hope that made sense.
2. I think this is merely an index origin 0 difficulty.
Using slice we avoid duplicate code.
Code:
```def pressbutton(i):
lightsarray = [False] * 50 ### All lights off at start ***
for count in range(1, i+1):
S = slice(count-1,len(lightsarray),count)
lightsarray[S] = [not y for y in lightsarray[S]]
return lightsarray

for i in range(4):
print(i, pressbutton(i)[:5])```
3. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
Nov 2013
Posts
2
Rep Power
0
Originally Posted by b49P23TIvg
I think this is merely an index origin 0 difficulty.
Using slice we avoid duplicate code.
Code:
```def pressbutton(i):
lightsarray = [False] * 50 ### All lights off at start ***
for count in range(1, i+1):
S = slice(count-1,len(lightsarray),count)
lightsarray[S] = [not y for y in lightsarray[S]]
return lightsarray

for i in range(4):
print(i, pressbutton(i)[:5])```
Thank you so much! It works now!