Unix -Python-courses-Solaris, Linux, FreeBSDPython-courses-python-exercises-python

Exercise 31

For given a list of integers L, write a Python algorithm that returns the list of tuples (n, m) verifying n + m <10. Example: if L = [11, 3, 2, 22, 4, 31, 18, 6, 12, 1, 7], the algorithm returns: the list: [(3, 3), (2, 3), (4, 3), (6, 3), (1, 3), (3, 2), (2, 2), (4, 2), (6, 2), (1, 2), (7, 2), (3, 4), (2, 4), (4, 4), (1, 4), (3, 6), (2, 6), (1, 6), (3, 1), (2, 1), (4, 1), (6, 1), (1, 1), (7, 1), (2, 7), (1, 7)]

Solution

def listTuples (L):
# initialization of the list of tuples
lTuples = []
for n in L:
for m in L:
if m + n <10:
lTuples.append ((m, n))
return lTuples
# Example
L = [11, 3, 2, 22, 4, 31, 18, 6, 12, 1, 7]
print (listTuples (L))
Younes Derfoufi
my-courses.net

Leave a Reply