Write a Python Function That Takes a String s as Input And Returns The Length of The Largest Streak of 0s in The String. For Example if The Input String is 10001000110000001 The Function Should Return 6

Hi, in this article you will learn about a Python program that counts the largest streak of zeros in a string.

Let's define a Python function that takes a string and  returns the largest streak present in a  given string



Example
===========

Let's take an example of an input string that is 10001000110000001,  and try to understand what exactly is the largest streak of zeros in the above string.

Let's take the first occurrences of one and zero 10001  if you  notice the occurrences of zeros are only three times so initially the largest streak is 3.

Now let's take another currency of one and zeros 10001.  if you again notice zeros are only 3 so again the largest streak of zeros is 3.

Finally let's take the end of the string 10000001. If you notice the number of zeros are 6 so if we compare 6 with 3, then 6 is a largest so the largest streak is 6

 

Write a Python Function That Takes a String s as Input And Returns The Length of The Largest Streak of 0s in The String. For Example, if The Input String is 10001000110000001, The Function Should Return 6

 

 

Write a Python Function That Takes a String s as Input And Returns The Length of The Largest Streak of 0s in The String. For Example, if The Input String is 10001000110000001, The Function Should Return 6

Define the function named as find_largest_streak()  by passing a string as an input to the Python function. Take two  integer variables largest_streak and occurrences and initialize them to zero.

def find_largest_streak(s):
   largest_streak = 0
   occurrence = 0




Use the for loop to iterate all the letters present in the string and use the statement to check if the letter is equal to 0.  if the letter is equal to zero, increment the occurrence counter variable.  use the else block to check if the largest_streak is less than the occurrences then swap it.  

Initialize the occurrence variable to 0 every iteration of the for loop.  Once the for loop completes, return the variable largest_streak  as the return value of the function.

   for letter in s:
       if letter == "0":
           occurrence += 1
       else:
           if largest_streak < occurrence:
               largest_streak = occurrence
           occurrence = 0
   return largest_streak




In the main program take a string variable named as text and ask a user to enter the string containing zeros and ones.  take a variable named as and call the function find_largest_streak()  by passing a text variable as an input.

Once the function gets executed it will return the largest tree and it will be stored in the result variable and use the print Function to print the largest streak.

text = input("Enter String Containing 0 and 1's : ")
result = find_largest_streak(text)
print("Largest Streak of 0s in The String is : ", result)





Complete Python Program
=========================

def find_largest_streak(s):
   largest_streak = 0
   occurrence = 0
   for letter in s:
       if letter == "0":
           occurrence += 1
       else:
           if largest_streak < occurrence:
               largest_streak = occurrence
           occurrence = 0
   return largest_streak


text = input("Enter String Containing 0 and 1's : ")
result = find_largest_streak(text)
print("Largest Streak of 0s in The String is : ", result)





Output 1
======================

Enter String Containing 0 and 1's : 10001000110000001
Largest Streak of 0s in The String is :  6




Output 2
======================

Enter String Containing 0 and 1's : 10101
Largest Streak of 0s in The String is :  1



Output 3
===================

Enter String Containing 0 and 1's : 1000100101
Largest Streak of 0s in The String is :  3



Conclusion
===============================

Execute the above Python program by providing a random string variable containing once and zeros and not down the result.

Comment down below if you have any queries related to the above Python program

Post a Comment

0 Comments