ADVERTISEMENT
ADVERTISEMENT

Python program for array rotation

Here, we are going to learn about the array rotation and writing a Python program for array rotation.
Submitted by Shivang Yadav, on April 04, 2021

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.

Array Rotation:

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

  1. Left Rotate
  2. Right Rotate

Left Rotate will shift all elements to left by one index and place the first index element to the last index.

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,

Method 1: By rotating the 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

Method 2: 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

Method 3: 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

Python Array Programs »



ADVERTISEMENT



ADVERTISEMENT



Comments and Discussions


ADVERTISEMENT

ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.