Signup/Sign In
PUBLISHED ON: JULY 2, 2021

Python Program for Array Rotation

In this tutorial, we will learn how to write a function for Array Rotation. The user will give an input d by which the elements of the array will be rotated.

Python arrays are data structures that store homogenous data. Arrays stores objects of the same datatype. Arrays are mutable, meaning that the data in an array can be changed and iterative, meaning each element in an array can be accessed one by one.

Suppose an array a[ ] has the following elements,

array

It has to be rotated by 2 then the resultant array will be,

array

Our program should give output as follows:

Input-[1, 2, 3, 4, 5, 6]

Output-Rotated array is [3, 4, 5, 6, 1, 2]

For executing this task we will follow these approaches:

  1. Using a temporary array that will store the shifted elements
  2. Slicing the array

Approach 1: By Using a temporary array

In this approach, we will declare a temporary array that will store the array elements in a shifted order.

Input arr[] = [1, 2, 3, 4, 5, 6, 7], d = 2, n =7

1) Store all the elements in the index range d to n in a temp array
   temp[] = [3, 4, 5, 6, 7]
2) Add all the elements in the index range 0 to d in the temp array
   arr[] = [3, 4, 5, 6, 7, 1, 2]
3) Copy the temp array to arr[]
   arr[] = [3, 4, 5, 6, 7, 1, 2]

Algorithm

Step 1- Define a function to rotate the array

Step 2- Declare a temporary variable

Step 3- Use len() to calculate the length of the array and store it in a variable

Step 4- Run a loop from d to n and store the elements at each index in the temp array

Step 5- Run another loop to add the rest of the elements to the temporary array

Step 6- Copy temp array to original array

Step 7- Return array

Python Program 1

Look at the program to understand the implementation of the above-mentioned approach.

def rotateArray(a,d):
    temp = []
    n=len(a)
    for i in range(d,n):
        temp.append(a[i])
    i = 0
    for i in range (0,d):
        temp.append(a[i])
    a=temp.copy()
    return a
 
arr = [1, 2, 3, 4, 5, 6, 7]
print("Array after left rotation is: ", end=' ')
print(rotateArray(arr, 2))


Array after left rotation is: [3, 4, 5, 6, 7, 1, 2]

Approach 2: Slicing

In this approach, we will use the concept of slicing. Since the array is a type of list with elements of the same data type we can use the concept of list slicing here. Through slicing, we can access any part of the array.

Algorithm

Step 1- Define a function to rotate elements in the array list

Step 2- Define the length of an array using len()

Step 3- Use splicing operator ( : ) to store the elements from d to n and 0 to d

Step 4- Use ( + ) to concatenate both arrays

Step 5- Return array

Python Program 2

Look at the program to understand the implementation of the above-mentioned approach.

# List slicing approch to rotate the array
def rotateArray(a,d):
    n=len(a)
    a[:]=a[d:n]+a[0:d]
    return a

arr = [1, 2, 3, 4, 5, 6]

print("Rotated list is")
print(rotateArray(arr,2)) 


Rotated list is
[3, 4, 5, 6, 1, 2]

Conclusion

In this tutorial, we learned the two approaches we can these to rotate elements in an array. We have used the concept of slicing in our tutorial. We can also declare a temporary array that will store the elements in the shifted order. To add data in the temporary array we have used append() and to copy the temporary array to the original array we have used copy() in our program.



About the author:
Nikita Pandey is a talented author and expert in programming languages such as C, C++, and Java. Her writing is informative, engaging, and offers practical insights and tips for programmers at all levels.