Count Consecutive 1s and 0s

 """ Write a program that will receive an input string of only 1s and 0s. Return 'yes' if all the counts of all conse...


 """

Write a program that will receive an input string of only 1s and 0s. Return 'yes' if all the counts of all consecutive 0s is even and count of all consecutive 1s is odd. Otherwise return 'no',


Example

Sample Input 1:

0010000111


Sample Output 1:

yes


Sample Input 2:

0100111


Sample Output 2:

no

"""


import time


# Method 1

input_string = input("Enter 0s and 1s only...\n")

start_time = time.time()

count0s = 0

count1s = 0

l0 = []

l1 = []

flag0 = 0

flag1 = 0

try:

    if input_string.isnumeric() and int(input_string, 2):

        for i in input_string:

            if int(i) == 0:

                count0s += 1

            else:

                l0.append(count0s)

                count0s = 0

        for i in input_string:

            if int(i) == 1:

                count1s += 1

            else:

                l1.append(count1s)

                count1s = 0

        l0.append(count0s)

        l1.append(count1s)

        for j in l0:

            if j > 0 and j % 2 != 0:

                flag0 = 1

                break

        for k in l1:

            if k > 0 and k % 2 == 0:

                flag1 = 1

                break

        if flag0 == 0 and flag1 == 0:

            print('Yes')

        else:

            print('No')

    else:

        print("Wrong input. Try again")

except:

    print("Wrong input. Try again")

print("Time taken : ", time.time() - start_time)


Output





Note: Feel free to comment with optimize solution

COMMENTS

Name

Accident Alert,1,AI,2,Array,1,Aurdino,1,C,2,Computer Graphics,9,Data Science,3,Dataset,1,Decoratot,1,Django,1,ESP32,1,Fixed point/iteration method,1,Greater or smaller,1,html,1,Image Processing,1,JAVA,1,Javascript,22,Machine Learning,1,Matlab,3,Numerical Method,13,OOP,1,Other,3,PHP,1,Point operation,1,Python,11,Raspberry pi,1,Recommendation System,1,Regression,1,Reservation System,1,Robotics,1,Simulation,2,sine wave,1,String Handling Function,1,Web scrap,1,Webpage,1,
ltr
item
COMPUTER PROGRAMMING: Count Consecutive 1s and 0s
Count Consecutive 1s and 0s
https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhIJNDfz6puBBL3twuTPFAmToaCffWIFM7_7d_3RqNYV8iBJTtB_91fZoX4PybWQPRnzLqHxceZy3fSLWjAFsnPnu94JxaZ9wOu-b4H9lyqWh3kGss4M5zRlqYGyi_qTOC78V8xocuf5N9s/w640-h248/Screenshot+from+2020-12-20+12-30-17.png
https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhIJNDfz6puBBL3twuTPFAmToaCffWIFM7_7d_3RqNYV8iBJTtB_91fZoX4PybWQPRnzLqHxceZy3fSLWjAFsnPnu94JxaZ9wOu-b4H9lyqWh3kGss4M5zRlqYGyi_qTOC78V8xocuf5N9s/s72-w640-c-h248/Screenshot+from+2020-12-20+12-30-17.png
COMPUTER PROGRAMMING
https://computerprogram4ru.blogspot.com/2020/12/count-consecutive-1s-and-0s.html
https://computerprogram4ru.blogspot.com/
https://computerprogram4ru.blogspot.com/
https://computerprogram4ru.blogspot.com/2020/12/count-consecutive-1s-and-0s.html
true
8672391763020279633
UTF-8
Loaded All Posts Not found any posts VIEW ALL Readmore Reply Cancel reply Delete By Home PAGES POSTS View All RECOMMENDED FOR YOU LABEL ARCHIVE SEARCH ALL POSTS Not found any post match with your request Back Home Sunday Monday Tuesday Wednesday Thursday Friday Saturday Sun Mon Tue Wed Thu Fri Sat January February March April May June July August September October November December Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec just now 1 minute ago $$1$$ minutes ago 1 hour ago $$1$$ hours ago Yesterday $$1$$ days ago $$1$$ weeks ago more than 5 weeks ago Followers Follow THIS CONTENT IS PREMIUM Please share to unlock Copy All Code Select All Code All codes were copied to your clipboard Can not copy the codes / texts, please press [CTRL]+[C] (or CMD+C with Mac) to copy