[Tutor] Consecutive_zeros

avi.e.gross at gmail.com avi.e.gross at gmail.com
Mon Jul 4 00:24:51 EDT 2022


I read through all the suggestions sent in to date and some seem a bit
beyond the purpose of the exercise. Why not go all the way and use a regular
expression to return all matches of "0+" and then choose the length of the
longest one!

But seriously, the problem is actually fairly simple as long as you start by
stating the maximum found so far at the start is 0 long. If no longer one is
found, then the answer will be 0.

And no need to play with lists. Keep a counter. Start at 0. When you see a
1, if the current count is greater that the current maximum, reset the
maximum. Either way reset the count to zero. When you see a zero, increment
the count. 

And critically, when you reach the end, check the current count and if
needed increment the maximum.

This is an exercise in a fairly small state machine with just a few states
as in a marking automaton or Turing machine. 

Is there a guarantee for the purposes of this assignment that there is
nothing else in the string and that it terminates? If there may be other
values or it terminates in a NULL of some kind, you may have to adjust the
algorithm in one of many ways but I suspect the assignment is
straightforward.

JUST FOR FUN --- DO NOT USE THIS:

import re

def consecutive_zeros(string):
    if (matches := re.findall("0+", string)) == [] :
        return 0
    else:
        return (max([len(it) for it in matches]))

print(consecutive_zeros("1010010001000010000001"))    #6
print(consecutive_zeros("00000000000000000000000000"))    #26
print(consecutive_zeros("1111"))    #0
print(consecutive_zeros("begin1x00x110001END"))    #3
print(consecutive_zeros("Boy did you pick the wrong string!"))    #0

prints out:

6
26
0
3
0

REPEAT: This is not a valid way for a normal assignment and would not have
been shown if I had not just finished a huge tome on using regular
expressions for everything imaginable and with very different
implementations in all kinds of programs and environments. But it is a tad
creative if also wasteful and does handle some edge cases. And note this may
not work in older versions of python as it uses the walrus operator. That
could easily be avoided with slightly longer code or different code but it
does present a different viewpoint on what a stretch of zeroes means.

-----Original Message-----
From: Tutor <tutor-bounces+avi.e.gross=gmail.com at python.org> On Behalf Of
Anirudh Tamsekar
Sent: Sunday, July 3, 2022 5:06 PM
To: tutor at python.org
Subject: [Tutor] Consecutive_zeros

Hello All,

Any help on this function below is highly appreciated.
Goal: analyze a binary string consisting of only zeros and ones. Your code
should find the biggest number of consecutive zeros in the string.

For example, given the string:
Its failing on below test case

print(consecutive_zeros("0"))
It should return 1. Returns 0

I get the max(length) as 1, if I print it separately


def consecutive_zeros(string):
    zeros = []
    length = []
    result = 0
    for i in string:
        if i == "0":
            zeros.append(i)        else:
            length.append(len(zeros))
            zeros.clear()
            result = max(length)
    return result



-Thanks,

Anirudh Tamsekar
_______________________________________________
Tutor maillist  -  Tutor at python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor



More information about the Tutor mailing list