Exercise 94*

Write a Python program as function that takes as parameter a string s and which returns the list repeated word within the string s. Example: if s = "Python is object oriented programming language. Python is also also used for data sciences", the fuction returns the list: ['Python', 'is', 'also']

Solution

def repeated_word(s):
# converting the string s to a list
L = s.split()
# initialising the list of repeated words
repeated = []

for word in L:
if L.count(word) >= 2 and word not in repeated:
repeated.append(word)
return repeated

# testing the algorithm
s = "Python is object oriented programming language. Python is also also used for data sciences"
print(repeated_word(s)) # the out put is: ['Python', 'is', 'also']

Younes Derfoufi
my-courses.net

Leave a Reply