Exercise 84

96 is an integer whose tens digit is divisible by 3 (property (*)) Given an integer n, write an algorithm in python which returns the list of integers less than or equal to n composed of two digits and checking the property (*)

Solution

# coding: utf-8
def listDiv3 (n):
# initialization of the list
lDiv3 = []
for i in range (0, n + 1):
# we test if the tens digit q = i//10 is divisible by 3
if (i//10)% 3 == 0 and (i//10) != 0:
lDiv3.append (i)
return lDiv3

# Example
n = 90
print (listDiv3 (n))
# The output is:
#[30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 90]
Younes Derfoufi
my-courses.net

Leave a Reply