Exercise 86
Write a Python script as fucntion which that takes a text T as input and returns the set of all the words that make up this text. Example if T = "Python is more power than Java", the algorithm returns the set:
{'Python', 'is', 'more', 'power', 'than', 'Java'}
Solution
"""
We can use Python's split() method to split the string into
words and then convert it into a set to get unique words.
"""
def extract_words(text):
# Split the text into words
words = text.split()
# Convert the list of words into a set to remove duplicates
unique_words = set(words)
return unique_words
# Example usage
s = "Python is more power than Java"
result = extract_words(s)
print(result)
#This will output:
{'than', 'power', 'Java', 'Python', 'more', 'is'}
Younes Derfoufi
CRMEF OUJDA
[…] Eercise 86 *|| Solution […]