Don’t worry, unlock all articles / blogs on PrepInsta by just simply logging in on our website
Python program for rotation of elements of array- left and right
February 23, 2022
Array Rotation Left and Right
In this program we’ll be learning about Python program for rotation of elements of array – left and right to a specified number of times. An array is said to be right rotated if all the selected elements were moved towards right by one position. The last element of array will become the first element of array after rotation and vice versa for left rotation.
Example
Input :arr[5] = [10, 20, 30, 40, 50] Output :
Right rotation by 1 position : 50 10 20 30 40
Left rotation by 1 position : 20 30 40 50 10
Method 1 :
In this method we will rotate the elements one by one by shifting them.
Declare a function leftRotate(arr, d, n)
Inside that function run a loop for range(d)
And call leftRotateOne(arr, n)
For function leftRotatebyOne(arr, n)
Store arr[0] in a variable say temp.
Run a loop for range(n-1), set arr[i] = arr[i+1]
At last set arr[n-1] to temp
Time and Space Complexity :
Time – Complexity : O(n*d)
Space – Complexity : O(1)
Method 1 : Code in Python
#Write a program for array rotation in Python
# Python3 program to rotate an array by
def leftRotate(arr, d, n):
for i in range(d):
leftRotatebyOne(arr, n)
# Function to left Rotate arr[] of size n by 1*/
def leftRotatebyOne(arr, n):
temp = arr[0]
for i in range(n-1):
arr[i] = arr[i + 1]
arr[n-1] = temp
# utility function to print an array */
def printArray(arr, size):
for i in range(size):
print ("% d"% arr[i], end =" ")
# Driver program to test above functions */
arr = [10, 20, 30, 40, 50, 60, 70]
leftRotate(arr, 2, 7)
printArray(arr, 7)
def leftrotation(brr,k):
arrlen=len(brr)
for i in range(k):
lastElement=brr[arrlen-1]
for j in range(arrlen-1,0,-1):
brr[j]=brr[j-1]
brr[0]=lastElement
return brr
def rightrotation(arr,k):
arrlen=len(arr)
for i in range(k):
firstElement=arr[0]
for j in range(arrlen-1):
arr[j]=arr[j+1]
arr[arrlen-1]=firstElement
return arr
arr=[int(x) for x in input(“Enter the number”).split()]
k=int(input(“Enter k: “))
print(“the left rotation”,leftrotation(arr[:],k))
print(“the right rotation”,rightrotation(arr[:],k))
a=list(map(int,input(“ENTER ARRAY ELEMENTS :”).split()))
b=int(input(“enter the number of left rotation :”))
for x in range(b):
a=a[1:len(a)]+a[0:1]
print(a)
arr=[1,2,3,4,5,6]
def left(arr,p):
for i in range(0,p):
x=arr[0]
arr.remove(x)
arr.append(x)
return arr
print(left(arr,3))
def rotate_left(arr,k):
rotate_arr = list(range(len(arr)))
for i in range(len(arr)):
rotate_arr[i] = arr[(i+k)%len(arr)]
return rotate_arr
arr = list(map(int,input().split()))
k = int(input())
print(rotate_left(arr,k))
Hey there, Thanks for commenting, kindly join our Discord server, our mentors will guide you further precisely will all your queries.🙌
def left_shift(arr,d,n):
y=[]
print(arr)
for i in range(n-d,n):
y.append(arr[i])
for j in range(0,n-d):
y.append(arr[j])
print(y)
x= [10, 20, 30, 40, 50]
n=len(arr)
left_shift(arr,2,n)
def leftrotation(brr,k):
arrlen=len(brr)
for i in range(k):
lastElement=brr[arrlen-1]
for j in range(arrlen-1,0,-1):
brr[j]=brr[j-1]
brr[0]=lastElement
return brr
def rightrotation(arr,k):
arrlen=len(arr)
for i in range(k):
firstElement=arr[0]
for j in range(arrlen-1):
arr[j]=arr[j+1]
arr[arrlen-1]=firstElement
return arr
arr=[int(x) for x in input(“Enter the number”).split()]
k=int(input(“Enter k: “))
print(“the left rotation”,leftrotation(arr[:],k))
print(“the right rotation”,rightrotation(arr[:],k))
x=list(map(int,input().split()))
n=int(input())
m=x[-n:]
m.extend(x[0:len(x)-n])
print(m)
l=list(map(int,input().split()))
n=int(input())
print(*(l[n:]+l[0:n]))
a=list(map(int,input(“ENTER ARRAY ELEMENTS :”).split()))
b=int(input(“enter the number of left rotation :”))
for x in range(b):
a=a[1:len(a)]+a[0:1]
print(a)