The objective is to create a boolean array, state, from another boolean array, initial, according to the rules described below.
If initial starts with False, the first and subsequent elements of state will be False. Upon reaching True in initial, state will switch to True.
Example:
initial: [False, False, False, True]
state: [False, False, False, True]
If, on the other hand, initial starts with True, the first and subsequent elements of state will also be True. Upon reaching True in initial, state will switch to False.
Example:
initial: [True, False, False, True]
state: [True, True, True, False]
If True is encountered in the middle of initial, the new value of state will depend on the previous state. For example, if the previous value of state was False, then state will switch to True:
initial: [False, False, False, True] --Take note of the last boolean
state: [False, False, False, True] --Take note of the last state i.e., False
On the other hand, if the previous value of state was True, then state will switch to False:
initial: [False, False, False, True] --Take note of the last boolean
state: [True, True, True, False] --Take note of the last state i.e., False
Based on these requirements and two simple samples of initial, I've written the following code:
import numpy as np
sample_1 = [False, False, False, False, False, True, False, False, False, True, False, False, False]
# sample_2 = [True, False, False, False, False, True, False, False, False, True, False, False, False]
expected_output = []
counter = 0
for x in np.array(sample_1):
if x == False and counter == 0:
idx = False
elif x == True and counter == 0:
idx = True
counter = 2
elif x == False and counter == 1:
idx = True
elif x == True and counter == 1:
idx = True
counter = 0
elif x == True and counter == 2:
idx = False
counter = 0
expected_output.append(idx)
The expected output, respectively for sample1 and sample2 is:
For sample_1:
[False, False, False, False, False, True, True, True, True, False, False, False, False]
For sample_2:
[True, True, True, True, True, True, False, False, False, True, True, True, True]
I am curios whether there is more compact notation or a build-in module that can be used instead of the naive if-else approach above.
sample_2beFalseaccording to your computation rules?