Home  >  Article  >  Backend Development  >  Python program to rotate array elements

Python program to rotate array elements

WBOY
WBOYforward
2023-09-01 15:05:061205browse

Python program to rotate array elements

When an array is declared, the array elements up to a specific index are rotated so that the first element before the desired index is placed next to the last element in the last array. Let's discuss this through an input-output scenario.

Input and output scenarios

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 last element has index 9.

  • Suppose the array is rotated by two elements.

  • In this example, the first two elements are placed after the last element "10".

  • First, element "1" will be placed after 10, after placing element "1", 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 will discuss the process of rotating certain elements of an array (all at once) a certain number of times. The steps you must follow to build your program are as follows:

  • Declare a function or method that handles the rotation of array elements.

  • (Note that the parameters of this method must consist of an array, the maximum size of the array, and the number of rotations required by the user)

  • In this method, consider a new array with variable name "temp" to store the rotated array elements.

  • With the help of variable "i" and a loop, iterate over the elements of the array (until an index equal to the number of rotations) and append the elements one by one to the "temp" array.

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

  • Now, merge the array "arr" into 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 of 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 will discuss the process of rotating certain elements in an array (one after another) a certain number of times. The steps that must be followed to build the program are as follows -

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

  • (Note that the parameters of this method must consist of an array, the maximum size of the array, and the number of rotations required by the user)

  • In the second approach, consider an empty array with a variable named "temp" to store the rotated array elements.

  • With the help of variable " i " and a loop, iterate over all the elements from index 0 to the last index of the last element and rotate the elements of the array in sequence.

  • Print the elements present in the rotated array "arr".

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 of 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]

in conclusion

We can clearly observe that the two programs discussed above produce exactly equal outputs. The only difference between programs is the process followed and the methods used within the body of the program. In the first program, in a single method, the elements are rotated together using an external array. In the second program, two different methods are used to rotate the elements sequentially by calling a method. This allows you to rotate the elements of the array.

The above is the detailed content of Python program to rotate array elements. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete