Python program for array rotation

Here, we are going to learn about the array rotation and writing a Python program for array rotation. By Shivang Yadav Last updated : March 05, 2024

Python programming language is a high-level and object-oriented programming language. Python is an easy to learn, powerful high-level programming language. It has a simple but effective approach to object-oriented programming.

What Is Array Rotation?

It is the process of shifting elements of the array. There are two types of array rotations:

  1. Left Rotate: Left Rotate will shift all elements to left by one index and place the first index element to the last index.
  2. Right Rotate: Right Rotate will shift all elements to right by one index and place the last index element to the first index.

Here, we will take the array as input from the user and then the number of rotations to be done. We need to create a program in python to left rotate the elements of the array.

There can be multiple ways to rotate an array in python. Let's discuss them here,

Array Rotation By Rotating Array By One, R times

One rotation can be done by storing the first element, shifting all rest elements by one and then storing the first element to the last position.

We will repeat these steps R times to perform the task.

Algorithm

  1. Get the array from the user.
  2. Repeat steps 3 to 5, R times.
  3. Initialize firstEle = arr[0].
  4. Loop from 0 to n-1
    • arr[i] = arr[i + 1].
  5. Initialise arr[n] = firstEle.
  6. Return the array.

Program to rotate the array R times

# Python program to left rotate array

# Function to rotate arrays in Python
def rotateArrayLeft(arr, R, n):
    for i in range(R):
    	firstVal = arr[0]
    	for i in range(n-1):
    		arr[i] = arr[i+1]
    	arr[n-1] = firstVal

# Taking array input from user
arr = [1, 2, 3, 4, 5, 6, 7]
n = int(input("Enter number of elements of the array: "))
arr = []
print("Enter elements of the array :")
for i in range(n):
  numbers = int(input())
  arr.append(numbers)
R = int(input("Enter rotation count: "))

# Printing array 
print("Initial Array :", end = " ")
for i in range(n):
	print ("%d"% arr[i],end=" ")
	
# Calling function for left Rotating array 
rotateArrayLeft(arr, R, n)

# Printing new array 
print("\nArray after rotation: ", end = " ")
for i in range(n):
	print ("%d"% arr[i],end=" ")

Output:

Enter number of elements of the array: 10
Enter elements of the array :
12
23
34
45
56
67
78
90
10
67
Enter rotation count: 3
Initial Array : 12 23 34 45 56 67 78 90 10 67
Array after rotation:  45 56 67 78 90 10 67 12 23 34

Array Rotation Using an Extra Array to Store Elements

In this method, we will use an extra array of size R, to store the elements for rotation. We will store the elements in an extra array and then shift every element's R places left. Then put the elements of the array after the last element of after shift.

Algorithm

  1. Store first R elements of the array arr[] to an array shift[].
  2. Loop through the array i -> 0 to n-R.
    • arr[i] = arr[i + R].
  3. Append elements of shift[] at the end of arr[].
  4. Print the array formed after rotation.

Program to rotate array using an extra array

# Python program to left rotate array

# Function to rotate arrays in Python
def rotateArrayLeft(arr, R, n):
    shift = []
    i = 0
    while (i < R):
        shift.append(arr[i])
        i = i + 1
    for i in range(n-R):
    		arr[i] = arr[i+R]
    i = n-R
    j = 0
    while (i < n):
        arr[i] = shift[j]
        j += 1
        i = i + 1
    
    return arr

# Taking array input from user
arr = [1, 2, 3, 4, 5, 6, 7]
n = int(input("Enter number of elements of the array: "))
arr = []
print("Enter elements of the array: ")
for i in range(n):
  numbers = int(input())
  arr.append(numbers)
R = int(input("Enter rotation count: "))

# Printing array 
print("Initial Array: ", end = " ")
for i in range(n):
	print ("%d"% arr[i],end=" ")
	
# Calling function for left Rotating array 
rotateArrayLeft(arr, R, n)

# Printing new array 
print("\nArray after rotation: ", end = " ")
for i in range(n):
	print ("%d"% arr[i],end=" ")

Output:

Enter number of elements of the array: 10
Enter elements of the array:
10
20
30
40
50
60
70
80
90
100
Enter rotation count: 3
Initial Array:  10 20 30 40 50 60 70 80 90 100
Array after rotation:  40 50 60 70 80 90 100 10 20 30

Array Rotation Using Inbuilt Slicing on List

The list slice can directly perform the task as we can slice the array from any index into half and use the sliced part as an array.

Program to rotate array using list slicing

# Python program to left rotate array

# Function to rotate arrays in Python
def rotateArrayLeft(arr, R, n):
    arr[:]=arr[R:n]+arr[0:R]
    return arr

# Taking array input from user
arr = [1, 2, 3, 4, 5, 6, 7]
n = int(input("Enter number of elements of the array: "))
arr = []
print("Enter elements of the array: ")
for i in range(n):
  numbers = int(input())
  arr.append(numbers)
R = int(input("Enter rotation count: "))

# Printing array 
print("Initial Array: ", end = " ")
for i in range(n):
	print ("%d"% arr[i],end=" ")
	
# Calling function for left Rotating array 
rotateArrayLeft(arr, R, n)

# Printing new array 
print("\nArray after rotation: ", end = " ")
for i in range(n):
	print ("%d"% arr[i],end=" ")

Output:

Enter number of elements of the array: 10
Enter elements of the array:
10
20
30
40
50
60
70
80
90
100
Enter rotation count: 3
Initial Array:  10 20 30 40 50 60 70 80 90 100
Array after rotation:  40 50 60 70 80 90 100 10 20 30

To understand the above programs, you should have the basic knowledge of the following Python topics:

Python Array Programs »


Comments and Discussions!

Load comments ↻






Copyright © 2024 www.includehelp.com. All rights reserved.