Python Program to Rotate Elements of an Array


After the declaration of an array, the elements of the array until certain index are rotated such that the first elements until the desired index are placed in the last, next to the last element of the array. Let us discuss this with an Input output scenario.

Input Output Scenario

Consider an array arr = [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 ].

  • We can clearly say that the initial array consists of 10 elements and the index of the last element is 9.

  • Let us assume that the array is rotated by two elements.

  • In this case, the first two elements are placed after the last element “ 10 ”.

  • Firstly, the element “ 1 ” will be placed after 10 and after the element “ 1 ” is placed, then the next element “ 2 ” will be placed next to 1.

So, the resulting array will be arr = [ 3, 4, 5, 6, 7, 8, 9, 10, 1, 2 ].

Example

In this example, we are going to discuss about the process of rotating certain elements ( all at a time ) of an array by certain number of rotations. The steps that must be followed to construct a program are as follows:

  • Declare a function or a method that deals with rotating the elements of the array.

  • ( Note that the parameters of the method must consist of the array, the maximum size of the array and the number of rotations that the user require )

  • Within the method, consider a new array with a variable name “ temp ” in order to store the array elements after rotation.

  • With the help of a variable “ i ” and a loop, iterate the elements ( up to the index which is equal to number of rotations ) of the array and append the elements one after another into the “ temp ” array.

  • Consider another loop and iterate the elements from the next index and store them accordingly.

  • Now, merge the array “ arr ” to the array “ temp ” and store the value into the array “ arr ”.

def rotate_elements(arr, max, no_of_elements):
   temp = []
   i = 0
   while (i < no_of_elements):
      temp.append(arr[i])
      i += 1

   i = 0
   while (no_of_elements < max):
      arr[i] = arr[no_of_elements]
      i = i + 1
      no_of_elements = no_of_elements + 1

   arr[:] = arr[: i] + temp
   return arr
arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
print("The array before rotation is: ", end =' ')
print(arr)
print("The array after rotation is: ", end=' ')
max_size = len(arr)

print(rotate_elements(arr, max_size, 2))

Output

The output for the above program is as follows −

The array before rotation is:  [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
The array after rotation is:  [3, 4, 5, 6, 7, 8, 9, 10, 1, 2]

Example

In this example, we are going to discuss about the process of rotating certain elements ( one by one ) of an array by certain number of rotations. The steps that must be followed to construct a program are as follows −

  • Declare two functions. The first function will be used to iterate all the elements until the total number of rotations and simultaneously, it calls the second method such that after iterating the element, the element will be allowed to be rotated immediately.

  • ( Note that the parameters of the method must consist of the array, the maximum size of the array and the number of rotations that the user require )

  • In the second method, consider an empty array with a variable name “ temp ” in order to store the array elements after rotation.

  • Within the help of a variable “ i ” and a loop, iterate all the elements from the index 0 to the last index of the last element and rotate the elements of the array one after another.

  • Print the elements present in the array “ arr ” after rotation.

def rotate_elements(arr, no_of_elements, max):
   for i in range(no_of_elements):
      rotate_one_by_one(arr, max)
def rotate_one_by_one(arr, max):
   temp = arr[0]
   for i in range(max-1):
      arr[i] = arr[i+1]
   arr[max-1] = temp

arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
print("The array before rotation: ")
print(arr)

rotate_elements(arr, 2, 10)
print("The array after rotation: ")
print(arr)

Output

The output for the above program is as follows −

The array before rotation: 
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
The array after rotation:
[3, 4, 5, 6, 7, 8, 9, 10, 1, 2]

Conclusion

We can clearly observe that the output produced by both the programs discussed above are exactly equal. The only difference between the programs is the process followed within the program body and the methods used. In the first program, within a single method, the elements are rotated altogether by using an external array. In the second program, two different methods are used for rotating the elements one after another by calling a method. In this way, the elements of the arrays can be rotated.

Updated on: 08-May-2023

612 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements