Python-courses-python-exercises-python-dictionary

Exercise 37

Write a python algorithm that extracts from a given list of numbers the sublist made up of numbers that contain the digit 3.

Solution

# method which tests whether or not a number contains the digit 3
def contains3 (n):
# convert n to string
s = str (n)
if '3' in s:
return True
else:
return False

# method that determines the list of numbers that contain the digit 3

def listContains3 (L):
# initialization of the list of numbers that contain the digit 3
list3 = []
for n in L:
if contains3 (n):
list3.append (n)
return list3

# Example
L = [21, 137, 25, 31, 71, 239]
print (listContains3 (L)) # print [137, 31, 239]
Younes Derfoufi
my-courses.net

Leave a Reply