Exercise 47
Write a python algorithm that determines the list of all the words which contains at least one uppercase character in a given text T. Example if T = "Python is a high level programming language. Originally developed by Guido van Rossum in 1989." The program returns the list:
['Python', 'Originally', 'Guido', 'Rossum']
Solution
def find_uppercase_words(text):
words = text.split() # split the text into words
uppercase_words = [word for word in words if any(c.isupper() for c in word)]
return uppercase_words
# Example usage:
T = "Python is a high level programming language. Originally developed by Guido van Rossum in 1989."
uppercase_words = find_uppercase_words(T)
print(uppercase_words)
# Output:['Python', 'Originally', 'Guido', 'Rossum']
Explanation:
- The find_uppercase_words() function: takes a string text as input and returns a list of words containing at least one uppercase character.
- The split() method: convert the text into a list of words.
- Then, we use a list comprehension: to filter out words that do not contain any uppercase characters.
- For each word in the list of words: we check if any character in the word is uppercase using the isupper() method.
- If at least one character is uppercase: we include the word in the uppercase_words list.
- Finally: we return the uppercase_words list.
Younes Derfoufi
my-courses.net
[…] Exercise 47 || Solution […]