{"id":12413,"date":"2021-09-30T11:30:45","date_gmt":"2021-09-30T06:00:45","guid":{"rendered":"https:\/\/python-programs.com\/?p=12413"},"modified":"2021-11-22T18:35:28","modified_gmt":"2021-11-22T13:05:28","slug":"python-program-to-check-whether-given-array-or-list-can-form-arithmetic-progression","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-program-to-check-whether-given-array-or-list-can-form-arithmetic-progression\/","title":{"rendered":"Python Program to Check Whether given Array or List Can Form Arithmetic Progression"},"content":{"rendered":"
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 Arithmetic Progression:<\/strong><\/p>\n Arithmetic Progression (AP) is a numerical series in which the difference between any two consecutive numbers is a constant value. For instance, the natural number sequence: 1, 2, 3, 4, 5, 6,… is an AP with a common difference between two successive terms (say, 1 and 2) equal to 1. (2 -1). Even when dealing with odd and even numbers, we can observe that the common difference between two sequential terms is equal to 2.<\/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 Check Whether the given array or list can form the Arithmetic Progression series.<\/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 Grab the opportunity to learn all effective java programming language concepts from basic to advance levels by practicing these Java Program Examples with Output Arithmetic Progression: Arithmetic Progression (AP) is a numerical series in which the difference between any two consecutive numbers is a constant value. For instance, the natural number sequence: 1, 2, 3, …<\/p>\nGiven List = [9, 15, 21, 27, 33, 39, 45, 51, 57, 63, 69]<\/pre>\n
The given list [9, 15, 21, 27, 33, 39, 45, 51, 57, 63, 69] is in AP<\/pre>\n
Given List = [3, 9, 15, 21, 27, 33, 39, 45, 51, 57, 63, 69, 75, 81, 87, 93, 99, 105]<\/pre>\n
The given list [3, 9, 15, 21, 27, 33, 39, 45, 51, 57, 63, 69, 75, 81, 87, 93, 99, 105] is in AP<\/pre>\n
Python Program to Check Whether given Array or List Can Form Arithmetic Progression<\/h2>\n
\n
Method #1: Using For Loop (Static Input)<\/h3>\n
\n
# Create a function checkAp() which returns true if the given list\r\n# is in Arithmetic Progression Else it returns False.\r\n\r\n\r\ndef checkAp(gvnlst):\r\n # Calculate the length of the given list using the length() function.\r\n lstlen = len(gvnlst)\r\n # Inside the Function sort the given list using the built-in sort() function.\r\n gvnlst.sort()\r\n # Now compute the difference between the first two elements of the given list and\r\n # initialize a new variable commondif with this difference.\r\n commondif = gvnlst[1]-gvnlst[0]\r\n # Traverse the array in reverse order i.e from n-1 to 1 index using For loop.\r\n for m in range(lstlen-1, 1, -1):\r\n # Compare the difference between successive elements.\r\n\r\n if(gvnlst[m]-gvnlst[m-1] != commondif):\r\n # If the difference is not equal to commondif then return False.\r\n return 0\r\n # After the end of For loop return True (This signifies that the given list is in Ap)\r\n return 1\r\n\r\n\r\n# Give the list as static input and store it in a variable.\r\ngivenlist = [9, 15, 21, 27, 33, 39, 45, 51, 57, 63, 69]\r\n# Pass the given list as an argument to the checkAp() function.\r\nif(checkAp(givenlist)):\r\n print('The given list', givenlist, 'is in AP')\r\nelse:\r\n print('The given list', givenlist, 'is not in AP')\r\n<\/pre>\n
The given list [9, 15, 21, 27, 33, 39, 45, 51, 57, 63, 69] is in AP<\/pre>\n
Method #2: Using For Loop (User Input)<\/h3>\n
\n
# Create a function checkAp() which returns true if the given list\r\n# is in Arithmetic Progression Else it returns False.\r\n\r\n\r\ndef checkAp(gvnlst):\r\n # Calculate the length of the given list using the length() function.\r\n lstlen = len(gvnlst)\r\n # Inside the Function sort the given list using the built-in sort() function.\r\n gvnlst.sort()\r\n # Now compute the difference between the first two elements of the given list and\r\n # initialize a new variable commondif with this difference.\r\n commondif = gvnlst[1]-gvnlst[0]\r\n # Traverse the array in reverse order i.e from n-1 to 1 index using For loop.\r\n for m in range(lstlen-1, 1, -1):\r\n # Compare the difference between successive elements.\r\n\r\n if(gvnlst[m]-gvnlst[m-1] != commondif):\r\n # If the difference is not equal to commondif then return False.\r\n return 0\r\n # After the end of For loop return True (This signifies that the given list is in Ap)\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\ngivenlist = list(\r\n map(int, input('Enter some random List Elements separated by spaces = ').split()))\r\n# Pass the given list as an argument to the checkAp() function.\r\nif(checkAp(givenlist)):\r\n print('The given list', givenlist, 'is in AP')\r\nelse:\r\n print('The given list', givenlist, 'is not in AP')\r\n<\/pre>\n
Enter some random List Elements separated by spaces = 3 9 15 21 27 33 39 45 51 57 63 69 75 81 87 93 99 105\r\nThe given list [3, 9, 15, 21, 27, 33, 39, 45, 51, 57, 63, 69, 75, 81, 87, 93, 99, 105] is in AP<\/pre>\n
\n