DEV Community

HHMathewChan
HHMathewChan

Posted on • Originally published at rebirthwithcode.tech

Python exercise 7: Determine a string is a mixture of something

Question

  • Find words contain both alphabets and numbers from an input string.
  • Given
str1 = "Emma25 is Data scientist50 and AI Expert"
Enter fullscreen mode Exit fullscreen mode
  • Expected Output:
Emma25  
scientist50
Enter fullscreen mode Exit fullscreen mode

Hint

Use the built-in function any() with the combination of string method isalpha() and isdigit()

Syntax of any() function

any( iterable)
Enter fullscreen mode Exit fullscreen mode
  • Return True if any element of the iterable is true.
  • If the iterable is empty, return False.

My attempt

I can think use string.split() method first, and check each word in the with isalpha and isdigit method, but do not know how to combine them.

My Decomposition of the question

  • The output need to be an string composed of number and alphabet
  • The first step is to separate the str1 into different string
    • use split() method, this will return a list
  • check each string whether it is a mixture of digit and alphabet
    • if a string is mixture of digit and alphabet, isalpha() and isdigit() will both return True
  • if the part fulfil the requirement, output it

Recommend solution

str1 = "Emma25 is Data scientist50 and AI Expert"  

new_list = []  
# split string by whitespace  
temp_list = str1.split()  
# Check each string whether is a mixture of alphabet and digits  
for string in temp_list:  
    # If a string in a mixture of alphabet and digits, when checking each character in that string,  
    # isdigit and isalpha will return True at least one time,
    if any(char.isalpha() for char in string) and any(char.isdigit() for char in string):  
        # add the mixture string to a list  
        new_list.append(string)  

print("Displaying words with alphabets and numbers")  
# print out the result  
for string in new_list:  
    print(string)
Enter fullscreen mode Exit fullscreen mode

Algorithm of the solution

  1. initiate a new_list
  2. Separate the original list and store in the temp_list
  3. Check each string whether is a mixture of alphabet and digits
    • 3.1 If the string is a mixture of alphabet and digits, isdigit() and isalpha() both will return True at least one time
      • 3.1.1 check whether the string contain alpha
        • Use list comprehension to iterate over each character on the string, and check with isalpha() and output will present as an iterator
          • Call any() function on the iterator, a True should return
      • 3.1.2 Check whether a the string contain digit
        • Use list comprehension to iterate over each character on the string, and check with isdigit() and output will present as an iterator
          • Call any() function on the iterator, a True should return
      • 3.1.3 If both 3.1.1 and 3.1.2 are True, store the string to the new_list
  4. Print out the result
    • 4.1 Iterate over the new_list and print out each string

My reflection

I though I am close to solve this question, maybe don't give up too soon next time. Glad I learn a new function any() and how to use it.

Credit

Top comments (0)