Python program for finding the circular rotations of array by k positions
Circular rotation of an array by K position
Here in this program, we will learn about Python program for finding the circular rotations of array by k positions, which means rotating the elements in the array .The operation of the rotation moves the last element of the array to the first position and moves all the remaining elements to the right. Take into consideration the following [0,1,2] indexes to be checked.
Keypoint
In this section we will learn about basic knowledge which we need to know before coding the above Program. So we must have knowledge of what is an array?
Algorithm of Python program for finding the circular rotations of array by k positions
Algorithm
Step 1- Initialize a class
Step 2- Enter number of elements of array
Step 3- Enter number of rotations of array.
Step 4- Enter number of indexes to be displayed.
Step 5- Input array elements
Step 6- run a for loop, i=0; i< elements; , i++.
Step 7- then module your rotations with elements
Step 8- Enter the index of array to be displayed
Step 9- print number of indexes and rotations.
Code for Python program for finding the circular rotations of array by k positions
def rotateArray(arr, n, d): temp = [] i = 0 while (i < d): temp.append(arr[i]) i = i + 1 i = 0 while (d < n): arr[i] = arr[d] i = i + 1 d = d + 1 arr[:] = arr[: i] + temp return arr# Driver function to test above function arr = [1, 2, 3, 4, 5] print(“Array after left rotation is: “, end=’ ‘) print(rotateArray(arr, len(arr), 2))
Output
Array after left rotation is: [3, 4, 5, 1, 2]
Login/Signup to comment
arr=[1,2,3,4,5]
rot_count=int(input())
res=[]
if(rot_count==0 or rot_count%len(arr)==0):
res=arr
else:
left_part=rot_count%len(arr)
res=arr[left_part:]+arr[:left_part]
print(res)
Hey there,
Kindly join our Discord server, our mentors will guide you further with all your subject and technical queries.
l=[1,2,3,4,5]
k=4
for i in range(k):
temp=l[0]
for j in range(len(l)-1):
l[j]=l[j+1]
l[len(l)-1]=temp
print(l)
Join our TA Support Group on Discord, where we will help you out with all your technical queries:
📍https://prepinsta.com/discord/
If you are new to Discord, please watch our tutorial here❤️:
https://bit.ly/how-to-use-prepinsta-discord-server