Exercise 41
Write an algorithm in python that extracts from a list of numbers the sublist of numbers whose last digit is even and the second is odd. Example if: L = [21, 14, 346, 728, 136, 19], the algorithm returns the list: [14, 136]
Solution
def listEvenOdd (L):
# initialization of the list of numbers including the last even number and the next odd number
lEventOdd = []
for n in L:
# determine the last digit of the number n
last = n%10
second = ((n- last) // 10)% 10
# test if the last digit is even and the scond odd
if last%2 == 0 and second%2 != 0:
lEventOdd.append (n)
return lEventOdd
# Example
L = [21, 14, 346, 728, 136, 19]
print (listEvenOdd (L)) # which displays: [14, 136]
Younes Derfoufi
my-courses.net
my-courses.net