Exercise 37
Write a Python program that creates a list whose elements are the common words to two strings s1 and s2.
Solution
def commonWords(s1 , s2):
listCommonWords = []
# convert s1 and s2 to python lists
L1 = s1.split()
L2 = s2.split()
# browsing the list elements
for word in L1:
if( word in L2 and word not in listCommonWords):
listCommonWords.append(word)
return listCommonWords
# Exemple for testing algorithm
s1 = "Python is oriented object programming language"
s2 = "Python is a programming language created in 1991"
print("The common words : " , commonWords(s1,s2))
# display : The common words : ['Python', 'is', 'programming', 'language']
Younes Derfoufi
my-courses.net
my-courses.net