Eercise 86 *
Write an algorithm in Python which determines the set of words that make up a text string s. Example if s = "Python is more power than Java", the algorithm returns the set:
{Python, is, more, power, than, Java}
Solution
# creation of a function which returns all the words that make up a text string s.
def wordSet (s):
# initialize the set of words
Set = set ({})
# convert the string to a list
ListWords = s.split ()
# browse the words in the ListWords list
for word in ListWords:
Set.add (word)
return Set
# Example
s = "Python is more power than Java"
print (wordSet (s))
# output: {'is', 'power', 'Java', 'more', 'Python', 'than'}
Younes Derfoufi
my-courses.net
my-courses.net