Consider an array of bits, say
1 1 1 0 0 0 0 1 0 0 1 0 1 1 1 1 1 0 1 0
We call a contiguous subarray of length ≥ 5 a phase if at least 85% of the bits are the same and the first/last bits are both equal to the majority bit. Furthermore, we call a phase maximal if it is not a strict subarray of some other phase.
Here are the maximal phases of the above example:
1 1 1 0 0 0 0 1 0 0 1 0 1 1 1 1 1 0 1 0
-------------
-------------
-------------
As you can see, there are 3
maximal phases. On the other hand, this
1 1 1 0 0 0 0 1 0 0 1 0 1 1 1 1 1 0 1 0
---------
is not a maximal phase as it is a strict subarray of at least one other phase.
The challenge
Input is a sequence of ≥ 5 bits via STDIN, command line or function argument. The bits may come in as a string or an array.
You are to output a single integer, the number of maximal phases for the array, either printed via STDOUT or returned from a function.
Scoring
This is code-golf so the program in the fewest bytes wins.
Test cases
0 1 0 1 0 -> 0
0 0 0 0 0 -> 1
0 0 0 0 1 0 1 1 1 1 -> 0
0 0 0 0 0 1 0 1 1 1 1 1 -> 2
1 0 0 0 0 0 0 0 0 0 0 0 0 1 0 -> 1
0 0 0 0 0 1 0 0 0 0 0 1 0 0 0 0 0 1 0 -> 2
0 0 0 0 0 1 0 0 0 0 0 1 0 0 0 0 0 1 0 0 -> 1
0 1 0 1 0 0 1 0 1 0 1 0 0 0 1 1 1 1 0 1 0 0 1 1 0 0 0 1 1 0 -> 0
1 1 1 1 1 0 0 0 0 0 1 1 1 1 1 0 0 1 0 0 0 0 0 0 0 0 1 1 0 1 -> 4
0 1 1 0 0 0 0 0 0 0 0 0 0 0 1 0 1 1 1 1 1 1 1 0 1 0 0 0 0 0 -> 5
Here's the explanation for the last case:
0 1 1 0 0 0 0 0 0 0 0 0 0 0 1 0 1 1 1 1 1 1 1 0 1 0 0 0 0 0
---------------------------
-------------------------
-----------------
-----------------
-------------
Fun fact: This challenge came about from a data mining problem with the goal of detecting change in temporal data.
1 1 0 1 1
85% of 5 is 4.25 which is So length 5 would be impossible or should we round that down to 4? \$\endgroup\$0
and ending at the last one. \$\endgroup\$