{"id":10723,"date":"2021-09-30T16:00:25","date_gmt":"2021-09-30T10:30:25","guid":{"rendered":"https:\/\/python-programs.com\/?p=10723"},"modified":"2021-11-22T18:34:25","modified_gmt":"2021-11-22T13:04:25","slug":"python-program-to-right-rotate-a-list-by-r-times","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-program-to-right-rotate-a-list-by-r-times\/","title":{"rendered":"Python Program to Right Rotate a List by R Times"},"content":{"rendered":"
Interested in programming and want to excel in it by choosing the short ways. Then, practicing with the available Java Program list<\/a> is mandatory.<\/p>\n Right Rotation of a List:<\/strong><\/p>\n An array’s elements are shifted to the right when it is rotated to the right, as demonstrated in the image below. right rotation involves rotating the array’s elements clockwise to the provided number of places.<\/p>\n Examples:<\/strong><\/p>\n Example1:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Example2:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n There are several ways to right rotate the given list by r times to the right in Python some of them are:<\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n The above solution has a time complexity of, where n is the size of the input and r is the number of rotations.<\/p>\n i)Integer List<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n The above solution has a time complexity of, where n is the size of the input and r is the number of rotations.<\/p>\n ii)String List<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Related Programs<\/strong>:<\/p>\n Interested in programming and want to excel in it by choosing the short ways. Then, practicing with the available Java Program list is mandatory. Right Rotation of a List: An array’s elements are shifted to the right when it is rotated to the right, as demonstrated in the image below. right rotation involves rotating the …<\/p>\ngiven list =[3, 9, 1, 2, 4, 5, 11, 23]\r\nnumber of positions = 6<\/pre>\n
The list before rotating r times = [3, 9, 1, 2, 4, 5, 11, 23]\r\nThe list after rotating r times : [1, 2, 4, 5, 11, 23, 3, 9]<\/pre>\n
given list = ['good', 'morning', 'this', 'is', 'btechgeeks']\r\nnumber of positions = 3<\/pre>\n
The list before rotating r times = ['good', 'morning', 'this', 'is', 'btechgeeks']\r\nThe list after rotating r times : ['this', 'is', 'btechgeeks', 'good', 'morning']<\/pre>\n
Program to Right Rotate a List by r Times in Python<\/h2>\n
\n
Method #1:Using Indexing(Static Input)<\/h3>\n
\n
# function which accepts the giveen list as argument\r\n# and rotates the given list to the right by one time\r\n\r\n\r\ndef rightRotateByOne(given_list):\r\n # intializing last variable to the last element of the given list\r\n lastval = given_list[-1]\r\n # traversing length in reverse order of given list -1 times using for loop and reversed,range functions\r\n for i in reversed(range(len(given_list) - 1)):\r\n # initializing the next index element to the current index element\r\n given_list[i+1] = given_list[i]\r\n # Initialize the first value of the given list to the last value.\r\n given_list[0] = lastval\r\n\r\n\r\n# function which accepts the given list and number\r\n# of positions as arguments and rotate the given list r times here r=positions\r\ndef rightRotateList(given_list, positions):\r\n # Pass the given list to the rightRotateOne function inside the\r\n # for loop which rotates the list to the right one time.\r\n for numb in range(positions):\r\n rightRotateByOne(given_list)\r\n\r\n\r\n# Driver Code\r\n# Give the list as static input and store it in a variable.\r\ngiven_list = [3, 9, 1, 2, 4, 5, 11, 23]\r\nprint('The list before rotating r times = ', given_list)\r\n# Give the number of rotations r as static input and store it in another variable.\r\npositions = 6\r\n# Pass the given list and number of rotations as arguments to the rightRotateList function\r\n# which rotates the given list to the right by r positions.\r\nrightRotateList(given_list, positions)\r\n# print the list after rotating to the right r times\r\nprint('The list after rotating r times : ', given_list)\r\n<\/pre>\n
The list before rotating r times = [3, 9, 1, 2, 4, 5, 11, 23]\r\nThe list after rotating r times : [1, 2, 4, 5, 11, 23, 3, 9]<\/pre>\n
Method #2:Using Indexing(User Input)<\/h3>\n
\n
# function which accepts the giveen list as argument\r\n# and rotates the given list to the right by one time\r\n\r\n\r\ndef rightRotateByOne(given_list):\r\n # intializing last variable to the last element of the given list\r\n lastval = given_list[-1]\r\n # traversing length in reverse order of given list -1 times using for loop and reversed,range functions\r\n for i in reversed(range(len(given_list) - 1)):\r\n # initializing the next index element to the current index element\r\n given_list[i+1] = given_list[i]\r\n # Initialize the first value of the given list to the last value.\r\n given_list[0] = lastval\r\n\r\n\r\n# function which accepts the given list and number\r\n# of positions as arguments and rotate the given list r times here r=positions\r\ndef rightRotateList(given_list, positions):\r\n # Pass the given list to the rightRotateOne function inside the\r\n # for loop which rotates the list to the right one time.\r\n for numb in range(positions):\r\n rightRotateByOne(given_list)\r\n\r\n\r\n# Driver Code\r\n# Give the list as user input using map(),split(),list() and int functions.\r\n# Store it in a variable.\u00a0\r\ngiven_list = list(\r\n map(int, input('Enter some random list separated by spaces = ').split()))\r\nprint('The list before rotating r times = ', given_list)\r\n# Give the number of rotations r as user input using int(input())\r\n# and store it in another variable.\r\npositions = int(input('Enter some random number of positions = '))\r\n# Pass the given list and number of rotations as arguments to the rightRotateList function\r\n# which rotates the given list to the right by r positions.\r\nrightRotateList(given_list, positions)\r\n# print the list after rotating to the right r times\r\nprint('The list after rotating r times : ', given_list)\r\n\r\n<\/pre>\n
Enter some random list separated by spaces = 8 77 9 12 1 6 4 3 7 9\r\nThe list before rotating r times = [8, 77, 9, 12, 1, 6, 4, 3, 7, 9]\r\nEnter some random number of positions = 4\r\nThe list after rotating r times : [4, 3, 7, 9, 8, 77, 9, 12, 1, 6]<\/pre>\n
\n
# function which accepts the giveen list as argument\r\n# and rotates the given list to the right by one time\r\n\r\n\r\ndef rightRotateByOne(given_list):\r\n # intializing last variable to the last element of the given list\r\n lastval = given_list[-1]\r\n # traversing length of given list -1 times using for loop\r\n for i in reversed(range(len(given_list) - 1)):\r\n # initializing the next index element to the current index element\r\n given_list[i+1] = given_list[i]\r\n # intializing first value to the last value of the given list\r\n given_list[0] = lastval\r\n\r\n\r\n# function which accepts the given list and number\r\n# of positions as arguments and rotate the given list r times here r=positions\r\ndef rightRotateList(given_list, positions):\r\n # Pass the given list to the rightRotateOne function inside the\r\n # for loop which rotates the list to the right one time.\r\n for numb in range(positions):\r\n rightRotateByOne(given_list)\r\n\r\n\r\n# Driver Code\r\n#Give the string list as user input using split(),list() functions.\r\ngiven_list = list( input('Enter some random list separated by spaces = ').split())\r\nprint('The list before rotating r times = ', given_list)\r\n# Give the number of rotations r as user input using int(input())\r\n# and store it in another variable.\r\npositions = int(input('Enter some random number of positions = '))\r\n# Pass the given list and number of rotations as arguments to the rightRotateList function\r\n# which rotates the given list to the right by r positions.\r\nrightRotateList(given_list, positions)\r\n# print the list after rotating to the right r times\r\nprint('The list after rotating r times : ', given_list)\r\n<\/pre>\n
Enter some random list separated by spaces = good morning this is btechgeeks\r\nThe list before rotating r times = ['good', 'morning', 'this', 'is', 'btechgeeks']\r\nEnter some random number of positions = 3\r\nThe list after rotating r times : ['this', 'is', 'btechgeeks', 'good', 'morning']<\/pre>\n
\n