Exercise 10
Write an algorithm in python as a function which takes as a parameter a tuple (L, x) formed by a list L and an element x and which returns the position of the element x in the list L. The function must return -1 if element x is not in the list.
Solution
def examinList (L, x):
if x in L:
return L.index (x)
else:
return -1
# Example
L = [13, 9, 5, 23]
x = 5
y = 11
print (examinList (L, x)) # display 2
print (examinList (L, y)) # display -1
Younes Derfoufi
my-courses.net
my-courses.net