0

I have a list, e.g.

[True, True, True, True, False, False, True, False, True, True, False]

and I want to know the minimal count that True is successive, hence 1 in this case.

Or in in the following list it is 2,

[True, True, False, True, True, False, False, False, False, True, True, True] 

Thanks for the help!

Glenn C.
  • 119
  • 7
  • please share what you have tried – depperm Apr 27 '18 at 14:44
  • You can get the minimum of the value given in https://stackoverflow.com/questions/24342047/count-consecutive-occurences-of-values-varying-in-length-in-a-numpy-array – irene Jun 15 '18 at 12:58

3 Answers3

0

Here's my (naive) solution:

list_1 = [True, True, True, True, False, False, True, False, True, True, False]
list_2 = [True, True, False, True, True, False, False, False, False, True, True, True] 
list_3 = [False, False, False]
list_4 = [True, True, True]
list_5 = [False, False, False, True, True]

def min_successive_val_count(list_of_vals):
  list_of_runs = []
  counter = 0
  for val in list_of_vals:
    if val:
      counter+=1
    else:
      if counter != 0:
        list_of_runs.append(counter)
      counter = 0
  if list_of_runs:
    return min(list_of_runs)
  elif counter == len(list_of_vals):
    return counter
  elif list_of_vals[-1]:
    return counter
  else:
    return "No True's in your list"

print(min_successive_val_count(list_1))
print(min_successive_val_count(list_2))
print(min_successive_val_count(list_3))
print(min_successive_val_count(list_4))
print(min_successive_val_count(list_5))

Output:

1
2
No True's in your list
3
2

good luck (hopefully my last edit ;)

NBlaine
  • 493
  • 2
  • 11
0

From Python: A program to find the LENGTH of the longest run in a given list? but with max -> min.

import itertools
lst = [True, True, True, True, False, False, True, False, True, True, False]
x = min(sum(1 for _ in l) for n, l in itertools.groupby(lst))

>>> x
    1
Grant Williams
  • 1,469
  • 1
  • 12
  • 24
  • Nope, this does not care whether the shortest sequence consists of `False` or `True`. For example `[True, True, True, False]` will yield `x=1` – lkriener Apr 27 '18 at 15:44
0

lets use a dictionary to record our true counter while we traverse through this list.

list = [True, True, False, True, True, False, False, False, False, True, True, True]

currentCounter = 0

record = []

def getMin (list):   
    for x in list:
        if x:
            currentCounter[0] = currentCounter[0]+1
        else:
            record.append(currentCounter)
            currentCounter = 0
    return secondSmallest(record)

#we need to get the second smallet number because always the smallest is equal 0
def secondSmallest(numbers):
    m1, m2 = float('inf'), float('inf')
    for x in numbers:
        if x <= m1:
            m1, m2 = x, m1
        elif x < m2:
            m2 = x
    return m2     
lkriener
  • 187
  • 2
  • 7
Mohamed Abdou
  • 441
  • 3
  • 11