This is not a complete answer, but it is a start.
This uses the groupby()
method associated with the itertools
library. The groupby()
method looks for sequential groups of values (as opposed to true groups of values) thus it is ideally suited to finding sequences.
array = [1, 0, 0, 0, 1]
from itertools import groupby
g = groupby(array)
for value, grp in g:
grp
is an iterator... we can expose the contents by casting it with the list()
function to extract the values into a list.
grp = list(grp)
length = len(grp)
The if
statement using in
is a convenient method to check for various values.
if length in [3, 4, 5]:
print('number_same =', length)
print('element =', value)
print('positions =', 'still working on this')
==== OUTPUT ====
number_same = 3
element = 0
positions = still working on this