Exercise 28
Write a Python algorithm that determines the list of words containing at least one capital letter in a given T text.
Solution
# creation of a function which tests if a word contains a capital letter or not
def containsUppercase (word):
# initialize a counter
counter = 0
for x in word:
if (x.isupper()):
counter = counter + 1
if counter == 0:
return False
else:
return True
def listWordWithMaj (T):
# initialization of the list of words containing a capital letter
listMaj = []
# Convert T to a list
L = T.split ()
for word in L:
#calling the function above to test if the word contains a capital letter
if containsUppercase (word):
listMaj.append (word)
return listMaj
# Example
T = "laravelFramework is a PHP framework that works with mySql"
print (listWordWithMaj (T)) # print: ['laravelFramework', 'PHP', 'mySql']
Younes Derfoufi
my-courses.net
my-courses.net