w3resource

Python: Check if every consecutive sequence of zeroes is followed by a consecutive sequence of ones of same length


Check Zero-One Sequence

Write a Python program to check if every consecutive sequence of zeroes is followed by a consecutive sequence of ones of the same length in a given string. Return True/False.

Sample Solution:

Python Code:

# Import the 're' module for regular expressions.
import re

# Define a function 'test' that checks if every consecutive sequence of zeroes is followed by a consecutive sequence of ones.
def test(txt):
    # Use regular expressions to find all consecutive sequences of zeroes and ones and compare their lengths.
    return [len(i) for i in re.findall('0+', txt)] == [len(i) for i in re.findall('1+', txt)]

# Test the function with different input sequences.
str1 = "001011"
print("\nOriginal sequence:", str1)
print("Check if every consecutive sequence of zeroes is followed by a consecutive sequence of ones in the said string:")
print(test(str1))

str1 = "01010101"
print("\nOriginal sequence:", str1)
print("Check if every consecutive sequence of zeroes is followed by a consecutive sequence of ones in the said string:")
print(test(str1))

str1 = "00"
print("\nOriginal sequence:", str1)
print("Check if every consecutive sequence of zeroes is followed by a consecutive sequence of ones in the said string:")
print(test(str1))

str1 = "000111000111"
print("\nOriginal sequence:", str1)
print("Check if every consecutive sequence of zeroes is followed by a consecutive sequence of ones in the said string:")
print(test(str1))

str1 = "00011100011"
print("\nOriginal sequence:", str1)
print("Check if every consecutive sequence of zeroes is followed by a consecutive sequence of ones in the said string:")
print(test(str1))

str1 = "0011101"
print("\nOriginal sequence:", str1)
print("Check if every consecutive sequence of zeroes is followed by a consecutive sequence of ones in the said string:")
print(test(str1))

Sample Output:

Original sequence: 001011
Check if every consecutive sequence of zeroes is followed by a consecutive sequence of ones in the said string:
False

Original sequence: 01010101
Check if every consecutive sequence of zeroes is followed by a consecutive sequence of ones in the said string:
True

Original sequence: 00
Check if every consecutive sequence of zeroes is followed by a consecutive sequence of ones in the said string:
False

Original sequence: 000111000111
Check if every consecutive sequence of zeroes is followed by a consecutive sequence of ones in the said string:
True

Original sequence: 00011100011
Check if every consecutive sequence of zeroes is followed by a consecutive sequence of ones in the said string:
False

Original sequence: 0011101
Check if every consecutive sequence of zeroes is followed by a consecutive sequence of ones in the said string:
False

Explanation:

Here is a breakdown of the above Python code:

  • Importing the 're' module:
    • Import the "re" module for regular expressions.
  • Define the 'test' function:
    • Define a function "test()" that checks if every consecutive sequence of zeroes is followed by a consecutive sequence of ones.
  • Regular expression matching:
    • Use regular expressions to find all consecutive sequences of zeroes ('0+') and ones ('1+') in the input string.
  • Comparing lengths:
    • Compares the lengths of consecutive sequences of zeroes and ones to check the specified condition.

Flowchart:

Flowchart: Python - Check if every consecutive sequence of zeroes is followed by a consecutive sequence of ones of same length.

For more Practice: Solve these Related Problems:

  • Write a Python program to verify if every consecutive sequence of zeros in a binary string is followed by a sequence of ones of the same length.
  • Write a Python program to check if a binary string follows the pattern where each block of zeros is immediately matched by an equal block of ones.
  • Write a Python program to validate that in a given binary string, the lengths of consecutive zero sequences equal the lengths of the subsequent one sequences.
  • Write a Python program to test if a binary string has balanced consecutive sequences of zeros and ones using regex or iteration.

Go to:


Previous: Write a Python program to get the domain name using PTR DNS records from a given IP address.
Next: Write a Python program to print Emojis using unicode characters or CLDR (Common Locale Data Repository ) short names.

Python Code Editor:

Have another way to solve this solution? Contribute your code (and comments) through Disqus.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.