Exercise 70
Write a Python algorithm to test if a given list is symmetric using the reverse() method. Example: for L1 = [2, 5, 11, 5, 2] the algorithm returns True and for L2 = [2, 23, 11, 51, 7] the algorithm returns False.
Solution
def symetric (L):
# create a copy of L
reverseL = L.copy()
# creation of the list inverse
reverseL.reverse()
if reverseL == L:
return True
else:
return False
#Example
L1 = [2, 5, 11, 5, 2]
L2 = [2, 23, 11, 51, 7]
print (symetric (L1)) # display True
print (symetric (L2)) # display False
Younes Derfoufi
my-courses.net
my-courses.net