Exercise 10
Write an algorithm in python as a function which takes as a parameter a tuple (L, a) formed by a list L and an element 'a' and which returns the position of the element 'a' in the list L. The function must return -1 if the element 'a' is not in the list.
Solution
1 2 3 4 5 6 7 8 9 10 11 12 |
def examinList(L, a): if a in L: return L.index(a) else: return -1 # Example L = [13, 9, 12 , 5, 23] a = 12 b = 47 print(examinList(L,a)) # The output is: 2 print(examinList(L,b)) # The output is: -1 |
Younes Derfoufi
CRMEF OUJDA
1 thought on “Solution Exercise 10: python algorithm that returns the position of an element in a list”