Python Program to Check Whether given Array or List Can Form Arithmetic Progression

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, 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.

Examples:

Example1:

Input:

Given List = [9, 15, 21, 27, 33, 39, 45, 51, 57, 63, 69]

Output:

The given list [9, 15, 21, 27, 33, 39, 45, 51, 57, 63, 69] is in AP

Example2:

Input:

Given List = [3, 9, 15, 21, 27, 33, 39, 45, 51, 57, 63, 69, 75, 81, 87, 93, 99, 105]

Output:

The given list [3, 9, 15, 21, 27, 33, 39, 45, 51, 57, 63, 69, 75, 81, 87, 93, 99, 105] is in AP

Python Program to Check Whether given Array or List Can Form Arithmetic Progression

Below are the ways to Check Whether the given array or list can form the Arithmetic Progression series.

Method #1: Using For Loop (Static Input)

Approach:

  • Give the list as static input and store it in a variable.
  • Create a function checkAp() which returns true if the given list is in Arithmetic Progression Else it returns False.
  • Pass the given list as an argument to the checkAp() function.
  • Calculate the length of the given list using the length() function.
  • Inside the Function sort the given list using the built-in sort() function.
  • Now compute the difference between the first two elements of the given list and initialize a new variable commondif with this difference.
  • Traverse the array in reverse order i.e from n-1 to 1 index using For loop.
  • Compare the difference between successive elements.
  • If the difference is not equal to commondif then return False.
  • After the end of For loop return True (This signifies that the given list is in Ap)
  • The Exit of the Program.

Below is the implementation:

# Create a function checkAp() which returns true if the given list
# is in Arithmetic Progression Else it returns False.


def checkAp(gvnlst):
  # Calculate the length of the given list using the length() function.
    lstlen = len(gvnlst)
    # Inside the Function sort the given list using the built-in sort() function.
    gvnlst.sort()
    # Now compute the difference between the first two elements of the given list and
    # initialize a new variable commondif with this difference.
    commondif = gvnlst[1]-gvnlst[0]
    # Traverse the array in reverse order i.e from n-1 to 1 index using For loop.
    for m in range(lstlen-1, 1, -1):
      # Compare the difference between successive elements.

        if(gvnlst[m]-gvnlst[m-1] != commondif):
          # If the difference is not equal to commondif then return False.
            return 0
    # After the end of For loop return True (This signifies that the given list is in Ap)
    return 1


# Give the list as static input and store it in a variable.
givenlist = [9, 15, 21, 27, 33, 39, 45, 51, 57, 63, 69]
# Pass the given list as an argument to the checkAp() function.
if(checkAp(givenlist)):
    print('The given list', givenlist, 'is in AP')
else:
    print('The given list', givenlist, 'is not in AP')

Output:

The given list [9, 15, 21, 27, 33, 39, 45, 51, 57, 63, 69] is in AP

Method #2: Using For Loop (User Input)

Approach:

  • Give the list as user input using list(),map(),input(),and split() functions.
  • Store it in a variable.
  • Create a function checkAp() which returns true if the given list is in Arithmetic Progression Else it returns False.
  • Pass the given list as an argument to the checkAp() function.
  • Calculate the length of the given list using the length() function.
  • Inside the Function sort the given list using the built-in sort() function.
  • Now compute the difference between the first two elements of the given list and initialize a new variable commondif with this difference.
  • Traverse the array in reverse order i.e from n-1 to 1 index using For loop.
  • Compare the difference between successive elements.
  • If the difference is not equal to commondif then return False.
  • After the end of For loop return True (This signifies that the given list is in Ap)
  • The Exit of the Program.

Below is the implementation:

# Create a function checkAp() which returns true if the given list
# is in Arithmetic Progression Else it returns False.


def checkAp(gvnlst):
  # Calculate the length of the given list using the length() function.
    lstlen = len(gvnlst)
    # Inside the Function sort the given list using the built-in sort() function.
    gvnlst.sort()
    # Now compute the difference between the first two elements of the given list and
    # initialize a new variable commondif with this difference.
    commondif = gvnlst[1]-gvnlst[0]
    # Traverse the array in reverse order i.e from n-1 to 1 index using For loop.
    for m in range(lstlen-1, 1, -1):
      # Compare the difference between successive elements.

        if(gvnlst[m]-gvnlst[m-1] != commondif):
          # If the difference is not equal to commondif then return False.
            return 0
    # After the end of For loop return True (This signifies that the given list is in Ap)
    return 1


# Give the list as user input using list(),map(),input(),and split() functions.
# store it in a variable.
givenlist = list(
    map(int, input('Enter some random List Elements separated by spaces = ').split()))
# Pass the given list as an argument to the checkAp() function.
if(checkAp(givenlist)):
    print('The given list', givenlist, 'is in AP')
else:
    print('The given list', givenlist, 'is not in AP')

Output:

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
The given list [3, 9, 15, 21, 27, 33, 39, 45, 51, 57, 63, 69, 75, 81, 87, 93, 99, 105] is in AP

Related Programs: