![Python-courses-python-exercises-python-dictionary](https://1.bp.blogspot.com/-T3BongcoJFo/X4CftQxVL8I/AAAAAAAAB5U/Lf59WgNyhGo2FdogKqxv0PIy_Ef7XDPdgCLcBGAsYHQ/s0/my-python-courses-python-exercises-with-solutions.png)
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
my-courses.net