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

Exercise 33

Write a Python program as a function which takes a list L as a parameter and returns the list obtained by performing a circular permutation on the list L. Example if L = [41, 11, 34, 20, 18, 6], the program returns: the list: [6, 41, 11, 34, 20, 18]

Solution

def circularPermutation (L):
# initialization of the list obtained via a circular permutation of L
circlarList = []
for i in range (1, len (L)):
circlarList.append (L [i-1])
# insert the last element in the first position
circlarList.insert (0, L [-1])
return circlarList

L = [41, 11, 34, 20, 18, 6]
print (circularPermutation (L))
# Displays: [6, 41, 11, 34, 20, 18]
Younes Derfoufi
my-courses.net

Leave a Reply