## Monday, May 17, 2021

Solution Exercise 29: list of words containing at least one number #### Exercise 29

Write a Python algorithm that determines the list of words containing at least one digit 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 containing at least one digit
def digitInText (T):
# initialize the list of words containing at least one digit
listWordWithNumber = []
# convert text T to a list
L = T.split ()
for word in L:
# we test if the word 'word' contains a number
# using the digitInWord function defined previously
if digitInWord (word):
listWordWithNumber.append(word)
return listWordWithNumber
# Example
T = "Python2.7 has been replaced by Python3.X since 2018"
print ("the list of words containing at least one digit is \n", digitInText (T))
# the list returned is: ['Python2.7', 'Python3.X', '2018']``````

Younes Derfoufi
my-courses.net
{"ft_lang_label":"__label__en","ft_lang_prob":0.7416799,"math_prob":0.8333089,"size":1049,"snap":"2022-27-2022-33","text_gpt3_token_len":276,"char_repetition_ratio":0.13875598,"word_repetition_ratio":0.13294798,"special_character_ratio":0.25548142,"punctuation_ratio":0.11794872,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.98010886,"pos_list":[0,1,2,3,4],"im_url_duplicate_count":[null,null,null,null,null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2022-08-07T18:26:35Z\",\"WARC-Record-ID\":\"<urn:uuid:ebac5496-209f-4e1b-b18c-dcc264226042>\",\"Content-Length\":\"156780\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:3c96ee02-3872-4e23-a0a4-5ec920253495>\",\"WARC-Concurrent-To\":\"<urn:uuid:c433dc3d-1204-415e-a8ad-8ca88dfcc5ee>\",\"WARC-IP-Address\":\"142.251.45.115\",\"WARC-Target-URI\":\"https://www.my-courses.net/2021/05/solution-exercise-29-list-of-words.html\",\"WARC-Payload-Digest\":\"sha1:VMXLFWY3L4UP5SU6BQGV6SGI4IFMEAO2\",\"WARC-Block-Digest\":\"sha1:YA2RQ3H6F6SXKNHLNIKX4NICXFPSE4SX\",\"WARC-Identified-Payload-Type\":\"application/xhtml+xml\",\"warc_filename\":\"/cc_download/warc_2022/CC-MAIN-2022-33/CC-MAIN-2022-33_segments_1659882570692.22_warc_CC-MAIN-20220807181008-20220807211008-00226.warc.gz\"}"}