Python: Check if every consecutive sequence of zeroes is followed by a consecutive sequence of ones of same length
Python Basic - 1: Exercise-142 with Solution
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 re
def test(txt):
return [len(i) for i in re.findall('0+',txt)]==[len(i) for i in re.findall('1+',txt)]
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
Flowchart:

Visualize Python code execution:
The following tool visualize what the computer is doing step-by-step as it executes the said program:
Python Code Editor:
Have another way to solve this solution? Contribute your code (and comments) through Disqus.
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.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
Python: Tips of the Day
Returns the symmetric difference between two lists, after applying the provided function to each list element of both:
Example:
def tips_symmetric_difference_by(p, q, fn): _p, _q = set(map(fn, p)), set(map(fn, q)) return [item for item in p if fn(item) not in _q] + [item for item in q if fn(item) not in _p] from math import floor print(tips_symmetric_difference_by([4.2, 2.4], [4.6, 6.8],floor))
Output:
[2.4, 6.8]
- Weekly Trends
- Java Basic Programming Exercises
- SQL Subqueries
- Adventureworks Database Exercises
- C# Sharp Basic Exercises
- SQL COUNT() with distinct
- JavaScript String Exercises
- JavaScript HTML Form Validation
- Java Collection Exercises
- SQL COUNT() function
- SQL Inner Join
- JavaScript functions Exercises
- Python Tutorial
- Python Array Exercises
- SQL Cross Join
- C# Sharp Array Exercises
We are closing our Disqus commenting system for some maintenanace issues. You may write to us at reach[at]yahoo[dot]com or visit us at Facebook