0

The question on my assignment is as follows:

Write a function that takes, as an argument, a string, identified by the variable aString. If the string only contains digits 0 and 1, return the string formed by concatenating the argument with the string "is a binary string." Otherwise, return a string indicating the length of the argument, as specified in the examples that follow. Name this function AmIBinary(aString).

I am having trouble figuring out how to form a loop which searches through a string and determines whether or not the string is a binary string. I understand how to get the length of a string, I just don't understand how to figure out if it is a binary string.

2
  • 2
    re.match("[01]+",test_string) Commented Jan 29, 2018 at 4:16
  • 1
    Use a for loop. Something like for char in aString:. You should also take some time to go through the python tutorial Commented Jan 29, 2018 at 4:17

4 Answers 4

2

Try this !

import re
def AmIBinary(aString):
    #test_str='Vijay'
    if re.match("[01]+",aString):
        print('Binary string')
    else:
        print(len(test_str))

#AmIBinary("Vijay")
AmIBinary('010101')
Sign up to request clarification or add additional context in comments.

Comments

2

You can use a for loop through a string the same way you can use with a list of numbers.

For example:

numbers = [1, 2, 3, 4]

You can use:

for number in numbers:
    # Do something

In your case, you just need to switch the number list by the string you receive as an argument like this:

aString = '100100100'

for number in aString:
    # Check if there's a different number than '0' and '1'

Look that in this case you need to compare it with the string version of the number, because you're iterating a string, so each value you receive from the for loop will be a string too.

In the end, you'll probably have something like this:

def AmIBinary(aString):
    for number in aString:
        if number != '0' and number != '1':
            return len(aString)
    aString += 'is a binary string'
    return aString

As you can see I am iterating through the string to check if any element of it is different than '1' and '0', if it is, I just return the length of the string, otherwise, if I checked every value in the string and everything is fine, I concatenate 'is a binary string' to the original string and return it.

I hope this helps.

2 Comments

if number not in '10': would be a lot more readable/pythonic in my opinion.
@PatrickHaugh you're right, however I think that it's clearer to write this way for someone who is starting and that could use this example for other languages.
0

Try the build-in function remix without use loop.

def AmIBin(s):
    try:    
       if s.startswith('0b'):
           int(s, 2)
       else:   
           int('0b{}'.format(s), 2)
    except: 
        return False
    else:   
        return True

Comments

-1

A binary string has been defined as a string that only contains "0" or "1". So, how about checking each 'character' in the string, and if it's not a "0" or "1" you will know that the string is not a binary string.

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.