Skip to main content
Question Protected by CommunityBot
deleted 11 characters in body; edited title; deleted 1 character in body
Source Link
200_success
  • 145.7k
  • 22
  • 191
  • 481

Count Consecutive oneconsecutive ones in a binary list

There is a list o binary(1, 0) numberconsisting of zeroes and ones. I want to find out the numberlength of most consecutive onethe longest streak of ones. It's my solution, show me if you have bestIs there a better solution.?

def consecutive_one(data):
   one_list = []
   size = 0
   for num in data:
       if num == 1:
           one_list.append(num)
       elif num == 0 and size < len(one_list):
           size = len(one_list)
           one_list = []

    return size

if __name__ == '__main__':
    data = [0, 1, 0, 1, 1, 0, 0, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 0]
    print(consecutive_one(data))

Count Consecutive one in a binary list

There is a list o binary(1, 0) number. I want to find out the number of most consecutive one. It's my solution, show me if you have best solution.

def consecutive_one(data):
   one_list = []
   size = 0
   for num in data:
       if num == 1:
           one_list.append(num)
       elif num == 0 and size < len(one_list):
           size = len(one_list)
           one_list = []

    return size

if __name__ == '__main__':
    data = [0, 1, 0, 1, 1, 0, 0, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 0]
    print(consecutive_one(data))

Count consecutive ones in a binary list

There is a list consisting of zeroes and ones. I want to find out the length of the longest streak of ones. Is there a better solution?

def consecutive_one(data):
   one_list = []
   size = 0
   for num in data:
       if num == 1:
           one_list.append(num)
       elif num == 0 and size < len(one_list):
           size = len(one_list)
           one_list = []

    return size

if __name__ == '__main__':
    data = [0, 1, 0, 1, 1, 0, 0, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 0]
    print(consecutive_one(data))
Tweeted twitter.com/StackCodeReview/status/764364836176334849
Fix markdown indent that seems was accidentally broken
Source Link
janos
  • 113.1k
  • 15
  • 154
  • 396

There is a list o binary(1, 0) number. I want to find out the number of most consecutive one. It's my solution, show me if you have best solution.

def consecutive_one(data):
       one_list = []
       size = 0
       for num in data:
           if num == 1:
               one_list.append(num)
           elif num == 0 and size < len(one_list):
               size = len(one_list)
               one_list = []
    
        return size
    
    if __name__ == '__main__':
        data = [0, 1, 0, 1, 1, 0, 0, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 0]
        print(consecutive_one(data))

There is a list o binary(1, 0) number. I want to find out the number of most consecutive one. It's my solution, show me if you have best solution.

def consecutive_one(data):
       one_list = []
       size = 0
       for num in data:
           if num == 1:
               one_list.append(num)
           elif num == 0 and size < len(one_list):
               size = len(one_list)
               one_list = []
    
        return size
    
    if __name__ == '__main__':
        data = [0, 1, 0, 1, 1, 0, 0, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 0]
        print(consecutive_one(data))

There is a list o binary(1, 0) number. I want to find out the number of most consecutive one. It's my solution, show me if you have best solution.

def consecutive_one(data):
   one_list = []
   size = 0
   for num in data:
       if num == 1:
           one_list.append(num)
       elif num == 0 and size < len(one_list):
           size = len(one_list)
           one_list = []

    return size

if __name__ == '__main__':
    data = [0, 1, 0, 1, 1, 0, 0, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 0]
    print(consecutive_one(data))
edited title
Link
Marc-Andre
  • 6.8k
  • 5
  • 39
  • 65

Count Consecutive one in a binary list. any best way?

added 217 characters in body
Source Link
hizbul25
  • 183
  • 1
  • 1
  • 6
Loading
Source Link
hizbul25
  • 183
  • 1
  • 1
  • 6
Loading