Home > database >  check String for consecutive zero's
check String for consecutive zero's

Time:11-23

I want to make a func that return the amount of consecutive zero's.

print(consecutive_zeros("0100100001"))

should return 4

def consecutive_zeros(string):
    zeros = 0
    cache = 0
    for elem in range(len(string)):
        if string[elem] == "0":
            zeros  = 1
        elif string[elem] == "1":
            if zeros > cache:
                zeros = cache
                zeros = 0
        return cache

My func always returns 0 if i give it a string with numbers

for elem in range(len(string)):
    if string[elem] == "0":
        zeros  = 1

This should count every 0

elif string[elem] == "1":
        if zeros > cache:
            zeros = cache

This breaks the counter when 1 appearing and checks if the amount of the counted consecutive zeros is higher as the momentarily saved zeros

CodePudding user response:

I'm not really a Python guy, but I'd just use a regex here.

txt = "0100100001"

# Find all sub strings with 2 or more consecutive zeros
matches = re.findall("00 ", txt)

# Compute string lengths
lengths = map(lambda x: len(x), matches)

# Find Longest
max_zeros = max(lengths);

print(max_zeros)

CodePudding user response:

Here is a fix of your code:

def consecutive_zeros(string):
    zeros = 0
    cache = 0
    for elem in string:
        if elem == "0":
            zeros  = 1
            if zeros > cache:
                cache = zeros # this was inverted
        else:
                zeros = 0
    return cache # this was wrongly indented

consecutive_zeros("01001000010")

output: 4

I would personally use itertools.groupby and a comprehension:

from itertools import groupby 

s = '0100100001'

max(len(list(g)) for v,g in groupby(s) if v=='0')

output: 4

CodePudding user response:

You also need to check the current count after you finished the iteration, in case the max consecutive appearances is in the last sequence.

def consecutive_zeros(string):
    curr = 0
    max_consecutive_zeros = 0
    for ch in string:
        if ch == '0':
            curr  = 1
            continue
        else:
            if curr >= max_consecutive_zeros:
                max_consecutive_zeros = curr
            curr = 0
    if curr >= max_consecutive_zeros:
        max_consecutive_zeros = curr
    return maxmax_consecutive_zeros    
max_consecutive_zeros = consecutive_zeros("0100100001")

print(max_consecutive_zeros)
>> 4

CodePudding user response:

See @Nick B for a great (and fast!) answer.

But, if you want to use your solution;

  1. Your function returns after the first loop - move your return such that it's aligned with your for loop
def consecutive_zeros(string):
    zeros = 0
    cache = 0
    for elem in range(len(string)):
        if string[elem] == "0":
            zeros  = 1
        elif string[elem] == "1":
            if zeros > cache:
                zeros = cache
                zeros = 0
    return cache #Aligned it such that data is returned at last
  1. Your function returns cache which is never updated; you need to update that
def consecutive_zeros(string):
    zeros = 0
    cache = 0
    for elem in range(len(string)):
        if string[elem] == "0":
            zeros  = 1
        elif string[elem] == "1":
            if zeros > cache:
                cache = zeros #'zeros = cache' before
                zeros = 0
    return cache #Aligned it such that data is returned at last
  1. Your code does not reset zeros - only when zeros>cache. Move zeros=0 outside the if zeros > cache but such that's still in the elif string[elem] == "1"
def consecutive_zeros(string):
    zeros = 0
    cache = 0
    for elem in range(len(string)):
        if string[elem] == "0":
            zeros  = 1
        elif string[elem] == "1":
            if zeros > cache:
                cache = zeros #'zeros = cache' before
            zeros = 0 #Move it such that it's updated whenever we reset our counter
    return cache #Aligned it such that data is returned at last

and then it works

  1. Note, use for elem in string instead of for elem in range(len(string))

CodePudding user response:

In case your string just contains 0 and 1's, you could:

data = "0100100001"
print(max(len(x) for x in data.split('1')))

Print:

4
  • Related