{"id":12765,"date":"2021-09-30T16:30:42","date_gmt":"2021-09-30T11:00:42","guid":{"rendered":"https:\/\/python-programs.com\/?p=12765"},"modified":"2021-11-22T18:33:34","modified_gmt":"2021-11-22T13:03:34","slug":"python-program-to-find-a-fixed-point-in-a-given-array-or-list","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-program-to-find-a-fixed-point-in-a-given-array-or-list\/","title":{"rendered":"Python Program to Find a Fixed Point in a given Array or List"},"content":{"rendered":"
An array is a type of variable that can hold several values at once.<\/p>\n
One element in a fixed point array is given as if its value is the same as its index. If a value is present, the code will return it; otherwise, it will return -1. We have an array of x unique numbers sorted in ascending order in this. In the following code, we create a function that returns a fixed point integer and returns -1 if there is no fixed point integer. The fixed point index is defined as an index I such that array[i] equals i.<\/p>\n
Given a list, the task is to find a fixed point in the given list in Python if the fixed point is present in the given list then print -1.<\/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 a Fixed Point in the given list in Python.<\/p>\n Grab the opportunity to learn all effective java programming language concepts from basic to advance levels by practicing these Java Program Examples with Output<\/a><\/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 Related Programs<\/strong>:<\/p>\n An array is a type of variable that can hold several values at once. One element in a fixed point array is given as if its value is the same as its index. If a value is present, the code will return it; otherwise, it will return -1. We have an array of x unique …<\/p>\nGiven List = [19, 24, 25, 3, 81, 144, 600, 900, 225, 4, 9, 1, 16, 49, 23, 49, 25, 10, 25]<\/pre>\n
The Fixed point present in the given list is [ 3 ]<\/pre>\n
Given list = [7 , 8, 45, 19, 21, 23, 6 ,7, 5,]<\/pre>\n
The Fixed point present in the given list is [ 6 ]<\/pre>\n
Program to Find a Fixed Point in a given Array or List\u00a0Python<\/h2>\n
\n
Method #1: Using For Loop (Static Input)<\/h3>\n
\n
# Create a function findFixed() which accepts the given list\r\n# as an argument and returns -1\r\n# if the list does not contain a fixed point else print the fixed point.\r\n\r\n\r\ndef findFixed(gvnlst):\r\n # Inside the findFixed() function calculate the length of the given list.\r\n lstleng = len(gvnlst)\r\n # Loop till length of the given list using For loop.\r\n for m in range(lstleng):\r\n # Check if the element is equal to the index(iterator value of the For loop).\r\n if gvnlst[m] is m:\r\n # If it is true then return index.\r\n return m\r\n # Return -1 after the end of the For loop.\r\n return -1\r\n\r\n\r\n# Give the list as static input and store it in a variable.\r\ngvnlst = [19, 24, 25, 3, 81, 144, 600, 900,\r\n 225, 4, 9, 1, 16, 49, 23, 49, 25, 10, 25]\r\n# Pass the given list as an argument to the findFixed() function.\r\nfxdpoint = findFixed(gvnlst)\r\nprint('The Fixed point present in the given list is [', fxdpoint, ']')\r\n<\/pre>\n
The Fixed point present in the given list is [ 3 ]<\/pre>\n
Method #2: Using For Loop (User Input)<\/h3>\n
\n
# Create a function findFixed() which accepts the given list\r\n# as an argument and returns -1\r\n# if the list does not contain a fixed point else print the fixed point.\r\n\r\n\r\ndef findFixed(gvnlst):\r\n # Inside the findFixed() function calculate the length of the given list.\r\n lstleng = len(gvnlst)\r\n # Loop till length of the given list using For loop.\r\n for m in range(lstleng):\r\n # Check if the element is equal to the index(iterator value of the For loop).\r\n if gvnlst[m] is m:\r\n # If it is true then return index.\r\n return m\r\n # Return -1 after the end of the For loop.\r\n return -1\r\n\r\n\r\n# Give the list as user input using list(),map(),input(),and split() functions.\r\n# Store it in a variable.\r\ngvnlst = list(map(int, input(\r\n 'Enter some random List Elements separated by spaces = ').split()))\r\n# Pass the given list as an argument to the findFixed() function.\r\nfxdpoint = findFixed(gvnlst)\r\nprint('The Fixed point present in the given list is [', fxdpoint, ']')\r\n<\/pre>\n
Enter some random List Elements separated by spaces = 7 8 45 19 21 23 6 7 5\r\nThe Fixed point present in the given list is [ 6 ]<\/pre>\n
\n