2

I am trying to solve a python programming challenge that requires a program for checking if a string is binary. If the string is binary, it should return "true". Otherwise, it should return "false".

When I ran the code, it iterates through the string and prints either "true" or "false" depending on whether the value in the string is "0" and "1" or not. Even though I have tried a couple of methods I keep ending up with a vertical display of the boolean values.

binary = {'0', '1'}
def is_binary(string):
    for i in str(string):
        if i in binary:
            print('true')
        else:
            print('false')
            break
is_binary('101010')
is_binary('101210')

How can I modify the code to be able to print a single "true" statement when the string is binary and a single "false" statement when the string is not binary regardless of the length of the string?

1
  • 1
    Don't print, but return. Go through the string, if a digit is non binary, return False. If you reached the end of the string, it means that all digits are binary, hence return True Commented Jan 22, 2019 at 8:34

4 Answers 4

1

You do something like this:

def is_binary(str):
    is_binary = True
    try:
        int(str, 2)
    except ValueError:
        is_binary = False
    return is_binary


is_binary('0101010101010') # returns True
is_binary('24340101041042101010') # returns False

I have use int to convert to binary, Please refer here to learn

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you very much. I added a print statement when calling the function and I got exactly what I expected.
1

Try the below code:

def is_binary(bin_str):
    if (set(bin_str) - set(['1','0'])):
        return False
    else:
        return True

bin_str = "0110011"
print is_binary(bin_str)

bin_str = "011020"
print is_binary(bin_str)

Output:

True
False

Solution using OP's logic:

binary = {'0', '1'} 
def is_binary(string): 
    for i in str(string): 
        if i in binary: 
            continue 
        else: 
            return False 
    return True

print is_binary('101010') 
print is_binary('101210')

3 Comments

is_binary = lambda x: not bool(set(x) - {'0', '1'})
The Solution using OP's logic has helped me see where I was missing the point. I appreciate
Please consider upvoting, if the answer helped you.
1

The problem in your code is that you are printing if every character was in binary set which is NOT what you want. You want to print true only if all the characters were in binary. So, you only need to print('true') at the end of the for loop.

So, This should do it:

binary = {'0', '1'}
def is_binary(string):
    for i in str(string):
        if i not in binary:
            print('false')
            return
    print('true')
is_binary('101010')
is_binary('101210')

1 Comment

Thank you very much for your feedback. It was very helpful.
0

Short way

I stumble here and thought about this:

# Declare one true and one false vars
bb = '010010111010010001011100'
bb2 = '010010111010010001O11100' # there's a letter O here

bin_set = set('0','1') # A set with char '0' and char '1'

I also could have written bin_set = {'0','1'}

Then (I was in ipython 3):

In : set(bb) == bin_set
Out: True

In : set(bb2) == bin_set
Out: False

If a string is only composed with 0 and 1 then its set() will be equal to {'0','1'} (set representation in python)

hth

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.