Python-courses-python-exercises-games-Python-courses-

Exercise 30

Write a Python algorithm that determines the list of words containing no digits in a given T text.

Solution

# creation of a function which detects if a word contains a number or not
def digitInWord (word):
counter = 0
for x in word:
if x.isdigit():
counter = counter + 1
if counter> 0:
return True
else:
return False

# creation of a function which returns the list of words with no digits
def noDigitInText (T):
# initialize the list of words with no digits
listWordWithoutNumber = []
# convert text T to a list
L = T.split ()
for word in L:
# we test if the word 'word' does not contain any digits
# using the digitInWord function defined previously
if not digitInWord(word):
listWordWithoutNumber.append (word)
return listWordWithoutNumber
# Example
T = "Python2.7 has been replaced by Python3.X since 2018"
print ("the list of words with no digit is: ", noDigitInText (T))
# returns: the list of words with no digit is: ['has', 'been', 'replaced', 'by', 'since']
Younes Derfoufi
my-courses.net

Leave a Reply