Exercise 82

Write a program in Python which determines the list of words containing two successive identical characters in a character string s. Example if s = "Python is the most recommended programming language", the algorithm returns the list

 ["recommended", "programming"].

Solution

# function which tests if a word contains two successive characters
def successive(word):
# initialization of a counter
counter = 0
for i in range (0, len(word) - 1):
if word [i] == word [i + 1]:
counter = counter + 1
if counter == 0:
return False
else:
return True

# function which determines the list of words containing at least 2 successive characters
def list_successive(T):
# convert the string variable T to a list
L = T.split()
# initialization of the list of words containing at least 2 successive characters
list_successives = []

for word in L:
if successive(word):
list_successives.append (word)
return list_successives

# Example
word = 'The Python programming language is used by a large community'
print (list_successive(word))
# The output of the program is: ['programming', 'community']
Younes Derfoufi
my-courses.net

Leave a Reply