{"id":18273,"date":"2021-08-26T10:42:09","date_gmt":"2021-08-26T05:12:09","guid":{"rendered":"https:\/\/python-programs.com\/?p=18273"},"modified":"2021-11-22T18:37:15","modified_gmt":"2021-11-22T13:07:15","slug":"python-program-to-find-the-rotation-count-in-rotated-sorted-list","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-program-to-find-the-rotation-count-in-rotated-sorted-list\/","title":{"rendered":"Python Program to Find the Rotation Count in Rotated Sorted List"},"content":{"rendered":"
In the previous article, we have discussed Python Program to Sort a List in Wave Form<\/a> In simple terms, print the minimum index value to get the rotation count.<\/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 Below are the ways to find the rotation count in the rotated sorted list:<\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Explore more instances related to python concepts from\u00a0Python Programming Examples<\/a>\u00a0Guide and get promoted from beginner to professional programmer level in Python Programming Language.<\/p>\n In the previous article, we have discussed Python Program to Sort a List in Wave Form Consider a List of distinct numbers that are sorted in ascending order. The list has been rotated k times (clockwise). The task is to determine the value of k. In simple terms, print the minimum index value to get …<\/p>\n
\nConsider a List of distinct numbers that are sorted in ascending order. The list has been rotated k times (clockwise). The task is to determine the value of k.<\/p>\nGiven List = [7, 9, 11, 12, 5, 6, 1, 8]<\/pre>\n
The number of rotations of a gvn_lst [7, 9, 11, 12, 5, 6, 1, 8] = 6<\/pre>\n
Given List = [6, 3, 1, 4, 7]<\/pre>\n
The number of rotations of a gvn_lst [6, 3, 1, 4, 7] = 1<\/pre>\n
Program to Find the Rotation Count in Rotated Sorted List in Python<\/h2>\n
\n
Method #1: Using For Loop (Static Input)<\/h3>\n
\n
# Give the list as static input and store it in a variable.\r\ngvn_lst = [1, 3, 5, 7, 9]\r\n# Calculate the length of the given list using the len() function and\r\n# store it in another variable.\r\nlen_lst = len(gvn_lst)\r\n# Take a variable say \"minim_index\" and initialize it with the first element of\r\n# the given list.\r\nminim_index = gvn_lst[0]\r\n# Loop from 0 to the length of the given list using the for loop.\r\nfor itr in range(0, len_lst):\r\n # Check if the value of the variable \"minim_index\" is greater than the given list of\r\n # iterator value.\r\n if (minim_index > gvn_lst[itr]):\r\n # If the statement is true then assign the given list of iterator value to the \"minim_index\".\r\n minim_index = gvn_lst[itr]\r\n# Store the iterator value in a variable \"minim_index\".\r\n minim_index = itr\r\n# Print \"minim_index\" to get the number of rotations of a given list.\r\nprint(\"The number of rotations of a gvn_lst\", gvn_lst, \"=\", minim_index)\r\n<\/pre>\n
The number of rotations of a gvn_lst [1, 3, 5, 7, 9] = 1<\/pre>\n
Method #2: Using For loop (User Input)<\/h3>\n
\n
# Give the list as user input using list(),map(),input(),and split() functions.\r\n#Store it in a variable.\r\ngvn_lst = list(map(int, input(\r\n 'Enter some random List Elements separated by spaces = ').split()))\r\n# Calculate the length of the given list using the len() function and\r\n# store it in another variable.\r\nlen_lst = len(gvn_lst)\r\n# Take a variable say \"minim_index\" and initialize it with the first element of\r\n# the given list.\r\nminim_index = gvn_lst[0]\r\n# Loop from 0 to the length of the given list using the for loop.\r\nfor itr in range(0, len_lst):\r\n # Check if the value of the variable \"minim_index\" is greater than the given list of\r\n # iterator value.\r\n if (minim_index > gvn_lst[itr]):\r\n # If the statement is true then assign the given list of iterator value to the \"minim_index\".\r\n minim_index = gvn_lst[itr]\r\n# Store the iterator value in a variable \"minim_index\".\r\n minim_index = itr\r\n# Print \"minim_index\" to get the number of rotations of a given list.\r\nprint(\"The number of rotations of a gvn_lst\", gvn_lst, \"=\", minim_index)\r\n<\/pre>\n
Enter some random List Elements separated by spaces = 6 3 1 4 7\r\nThe number of rotations of a gvn_lst [6, 3, 1, 4, 7] = 1<\/pre>\n
\n