Skip to main content
added 427 characters in body
Source Link
grc
  • 19.1k
  • 9
  • 38
  • 80

Python 2, 149 bytes

a=input() l=len(a) n=p=0 for i in range(l): for j in range(l-1,i+3,-1): if(j>p)>(.15<sum(a[i:j+1])/(j+1.-i)+a[i]+a[j]<2.85):n+=1;p=j;break print n 

This sweepsThe first loop scans across the array from left to right. Each bit, checkingindexed by i, is checked to see if each bit canit could be the first bit in a maximal phase.

This is done by the inner loop, which scans from right to left. If the subarray between i and j is a phase, we increase the counter and move on. Otherwise, we keep going until the subarray becomes too small or j reaches the end of the previous maximal phase.

1 1 1 0 0 0 0 1 0 0 1 0 1 1 1 1 1 0 1 0 i -> <- j 

Example:

$ python phase.py [1, 1, 1, 0, 0, 0, 0, 1, 0, 0, 1, 0, 1, 1, 1, 1, 1, 0, 1, 0] 3 

Python 2, 149 bytes

a=input() l=len(a) n=p=0 for i in range(l): for j in range(l-1,i+3,-1): if(j>p)>(.15<sum(a[i:j+1])/(j+1.-i)+a[i]+a[j]<2.85):n+=1;p=j;break print n 

This sweeps across the array, checking if each bit can be the first in a maximal phase.

Example:

$ python phase.py [1, 1, 1, 0, 0, 0, 0, 1, 0, 0, 1, 0, 1, 1, 1, 1, 1, 0, 1, 0] 3 

Python 2, 149 bytes

a=input() l=len(a) n=p=0 for i in range(l): for j in range(l-1,i+3,-1): if(j>p)>(.15<sum(a[i:j+1])/(j+1.-i)+a[i]+a[j]<2.85):n+=1;p=j;break print n 

The first loop scans across the array from left to right. Each bit, indexed by i, is checked to see if it could be the first bit in a maximal phase.

This is done by the inner loop, which scans from right to left. If the subarray between i and j is a phase, we increase the counter and move on. Otherwise, we keep going until the subarray becomes too small or j reaches the end of the previous maximal phase.

1 1 1 0 0 0 0 1 0 0 1 0 1 1 1 1 1 0 1 0 i -> <- j 

Example:

$ python phase.py [1, 1, 1, 0, 0, 0, 0, 1, 0, 0, 1, 0, 1, 1, 1, 1, 1, 0, 1, 0] 3 
Source Link
grc
  • 19.1k
  • 9
  • 38
  • 80

Python 2, 149 bytes

a=input() l=len(a) n=p=0 for i in range(l): for j in range(l-1,i+3,-1): if(j>p)>(.15<sum(a[i:j+1])/(j+1.-i)+a[i]+a[j]<2.85):n+=1;p=j;break print n 

This sweeps across the array, checking if each bit can be the first in a maximal phase.

Example:

$ python phase.py [1, 1, 1, 0, 0, 0, 0, 1, 0, 0, 1, 0, 1, 1, 1, 1, 1, 0, 1, 0] 3