Sunday, November 17, 2024
Google search engine
HomeLanguagesPython – Maximum consecutive elements percentage change

Python – Maximum consecutive elements percentage change

Sometimes, while working with Python lists, we can have a problem in which we need to extract the maximum change of consecutive elements. This kind of problem can have application in domains such as Data Science. Let’s discuss certain ways in which this task can be performed.

Input : test_list = [4, 6, 7] 
Output : 50.0 

Input : test_list = [7, 7, 7, 7] 
Output : 0.0

Method #1: Using loop + zip() The combination of above functions can be used to solve this problem. In this, we combine elements with its successive element using zip(). The loop is used to perform computation in brute manner. 

Python3




# Python3 code to demonstrate working of
# Maximum consecutive elements percentage change
# Using zip() + loop
 
# initializing list
test_list = [4, 6, 7, 4, 2, 6, 2, 8]
 
# printing original list
print("The original list is : " + str(test_list))
 
# Maximum consecutive elements percentage change
# Using zip() + loop
res = 0
for x, y in zip(test_list, test_list[1:]):
    res = max((abs(x - y) / x) * 100, res)
 
# printing result
print("The maximum percentage change : " + str(res))


Output : 

The original list is : [4, 6, 7, 4, 2, 6, 2, 8]
The maximum percentage change : 300.0

Time Complexity: O(n) where n is the number of elements in the list “test_list”.  loop + zip() performs n number of operations.
Auxiliary Space: O(1), constant extra space is required

Method #2: Using recursion + max() This is yet another way in which this task can be performed. In this, instead of loop, we perform this task using recursion, tracking maximum at each call. 

Python3




# Python3 code to demonstrate working of
# Maximum consecutive elements percentage change
# Using zip() + loop
 
# helpr_fnc
def get_max_diff(test_list, curr_max = None):
    pot_max = (abs(test_list[1] - test_list[0]) / test_list[0]) * 100
    if curr_max :
        pot_max = max(curr_max, pot_max)
    if len(test_list) == 2:
        return pot_max
    return get_max_diff(test_list[1:], pot_max)
 
# initializing list
test_list = [4, 6, 7, 4, 2, 6, 2, 8]
 
# printing original list
print("The original list is : " + str(test_list))
 
# Maximum consecutive elements percentage change
# Using zip() + loop
res = get_max_diff(test_list)
 
# printing result
print("The maximum percentage change : " + str(res))


Output : 

The original list is : [4, 6, 7, 4, 2, 6, 2, 8]
The maximum percentage change : 300.0

Method #3 : Using list comprehension

Use a list comprehension to calculate the percentage change for each pair of consecutive elements in the input list. It then returns the maximum percentage change.

Step-by-step approach:

  • Create a list comprehension to calculate the percentage change between consecutive elements.
  • Find the maximum percentage change from the list.
  • Return the maximum percentage change.

Python3




def max_percentage_change(test_list):#define input
  #calculate percentage
    percentage_change = [(test_list[i+1] - test_list[i])/test_list[i] * 100 for i in range(len(test_list)-1)]
    #return result
    return max(percentage_change) if percentage_change else 0.0
#input
test_list = [4, 6, 7]
#print output
print(max_percentage_change(test_list))


Output

50.0

Time complexity: O(n), as it involves iterating over each element of the input list once. 
Space complexity: O(1), as it only needs to store a few variables to calculate the result.

Method #4: Using numpy library

Use the numpy library to compute the percentage change between consecutive elements in the list and then find the maximum value using the max() function.

Step-by-step approach:

  • Import the numpy library.
  • Convert the given list to a numpy array using the np.array() function.
  • Compute the percentage change between consecutive elements using the np.diff() function.
  • Divide the result by the first element of the original list and multiply it by 100 to get the percentage change.
  • Use the np.max() function to find the maximum percentage change.
  • Print the maximum percentage change.

Python3




import numpy as np
 
# initializing list
test_list = [4, 6, 7, 4, 2, 6, 2, 8]
 
# printing original list
print("The original list is : " + str(test_list))
 
# Using numpy library
arr = np.array(test_list)
percent_change = np.diff(arr)/arr[:-1]*100
res = np.max(np.abs(percent_change))
 
# printing result
print("The maximum percentage change : " + str(res))


OUTPUT : 
The original list is : [4, 6, 7, 4, 2, 6, 2, 8]
The maximum percentage change : 300.0

Time Complexity: O(n)
Auxiliary Space: O(n)

Method #5: Using itertools pairwise() function and max()

  • The pairwise function takes an iterable as input and returns a sequence of consecutive pairs of elements in the iterable. It uses the tee and zip_longest functions from the itertools module to achieve this.
  • The get_max_diff function takes a list as input and computes the maximum percentage change between consecutive elements using a generator expression and the max function.

Python3




from itertools import tee
 
# helpr_fnc
def pairwise(iterable):
    a, b = tee(iterable)
    next(b, None)
    return zip(a, b)
 
def get_max_diff(test_list):
    return max((abs(y - x) / x) * 100 for x, y in pairwise(test_list))
 
# initializing list
test_list = [4, 6, 7, 4, 2, 6, 2, 8]
 
# printing original list
print("The original list is : " + str(test_list))
 
# Maximum consecutive elements percentage change
# Using itertools pairwise() function and max()
res = get_max_diff(test_list)
 
# printing result
print("The maximum percentage change : " + str(res))


Output

The original list is : [4, 6, 7, 4, 2, 6, 2, 8]
The maximum percentage change : 300.0

Time complexity: O(n)
Auxiliary space: O(1)

Dominic Rubhabha-Wardslaus
Dominic Rubhabha-Wardslaushttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Recent Comments