Python – Program to accept only binary string

Python – Program to accept only binary string

In this problem a string is given, our task is to check the string is a binary string or not i.e. in the form of 1’s or 0’s or both 1’s and 0’s.

Examples:

Input : '01010101010'
Output : Accepted

Input : 'geeks101'
Output: Not Accepted

Approach 1: We firstly create a set of the characters present in the given string using a set() function and declare set s which contain ‘0’ and ‘1’. then we check for the following conditions :
1) set p is the same as set s or 2) set p contains only ‘0’ or 3) set p contains only ‘1’ .

Because we are using or short circuit operator so if any one condition is true from these 3 conditions then the result is true otherwise not. if the result is true then the string is accepted otherwise the string is not accepted.

Note: f-strings is applicable only for python 3.6 and python 3.6+

Let’s see the Python3 implementarion:

# function for checking the
# string is accepted or not
def check(string) :

    # set function convert string
    # into a set of characters.
    p = set(string)

    # declare set of '0','1' .
    s = {'0','1'}

    # check set p is the same as set s
    # or set p contains only '0'
    # or set p contains only '1'
    # or not, if anyone condition
    # is true then the string is accepted
    # otherwise not.
    if s == p or p == {'0'} or p == {'1'}:
        print( f"String: {string} is Accepted" )
    else :
        print( f"String: {string} is not Accepted" )


        
# main code
if __name__ == "__main__" :

    # input given
    string = "101010000111"

    # function calling
    check(string)

Output:

String: 101010000111 is Accepted

Approach 2: We firstly assign string ’01’ to the variable t and assign 0 to the count variable. after that we have to iterate over each character of the given string and check for each character, it is present in string t i.e. ’01’ or not if not present then we set 1 to the count variable and break out of the for loop otherwise iterate. after coming out of the for loop we check the value of the count variable, if the value of the count is 1 then the string is not accepted otherwise the string is accepted.

Let’s see the Python3 implementarion:

# function for checking the
# string is accepted or not
def check2(string) :

    # initialize the variable t
    # with '01' string
    t = '01'

    # initialize the variable count
    # with 0 value
    count = 0

    # looping through each character
    # of the string.
    for char in string :

        # check the character is present in
        # string t or not.
        # if this condition is true
        # assign 1 to the count variable
        # and break out of the for loop
        # otherwise pass.
        if char not in t :
            count = 1
            break
        else :
            pass
        
    # after coming out of the loop
    # check value of count is non-zero or not
    # if value is non-zero then condition is true
    # and string is not accepted
    # otherwise string is accepted
    if count :
        print( f"String: {string} is not Accepted" )
    else :
        print( f"String: {string} is Accepted" )



# driver code
if __name__ == "__main__" :
    
    # input given
    string = "00101010001010"

    # function calling
    check2(string)

Output:

String: 00101010001010 is Accepted

Leave a Reply

Your email address will not be published.

📢 Need further clarification or have any questions? Let's connect!

Connect 1:1 With Me: Schedule Call


If you have any doubts or would like to discuss anything related to this blog, feel free to reach out to me. I'm here to help! You can schedule a call by clicking on the above given link.
I'm looking forward to hearing from you and assisting you with any inquiries you may have. Your understanding and engagement are important to me!

This will close in 20 seconds