Python – Summation of consecutive elements power

Given a List, the task is to write a Python program to compute summation of power of consecutive elements occurrences frequency.
Examples:
Input : test_list = [2, 2, 2, 3, 3, 3, 3, 4, 4, 5]
Output : 110
Explanation : 2^3 + 3^4 + 4^2 + 5 = 110Input : test_list = [2, 2, 2, 3, 3, 3, 3, 4, 4]
Output : 105
Explanation : 2^3 + 3^4 + 4^2 = 105
Method #1 : Using loop
In this, we iterate for each element and test for next element, if found to be different, perform summation of power of consecutive elements, else add counter to get frequency to raise number to.
Python3
| # Python3 code to demonstrate working of# Summation of consecutive elements power# Using loop# initializing listtest_list =[2, 2, 2, 3, 3, 3, 3, 4, 4, 5]# printing original listsprint("The original list is : "+str(test_list))freq =1res =0foridx inrange(0, len(test_list) -1):    # adding powers    iftest_list[idx] !=test_list[idx +1]:        res =res +test_list[idx] **freq        freq =1    else:        freq +=1# catering for last elementres =res +test_list[len(test_list) -1] **freq# printing resultprint("Computed summation of powers : "+str(res)) | 
The original list is : [2, 2, 2, 3, 3, 3, 3, 4, 4, 5] Computed summation of powers : 110
Time Complexity: O(n)
Auxiliary Space: O(1)
Method #2 : Using defaultdict() + loop + sum()
In this we capture frequency using defaultdict using key value pairs and loop is used to iterate through each element. Next, summation is performed using sum().
Python3
| # Python3 code to demonstrate working of# Summation of consecutive elements power# Using defaultdict() + loop + sum()fromcollections importdefaultdict# initializing listtest_list =[2, 2, 2, 3, 3, 3, 3, 4, 4, 5]# printing original listsprint("The original list is : "+str(test_list))# getting frequencytemp =defaultdict(int)forele intest_list:    temp[ele] +=1temp =dict(temp)# computing summationres =sum([key **temp[key] forkey intemp])# printing resultprint("Computed summation of powers : "+str(res)) | 
The original list is : [2, 2, 2, 3, 3, 3, 3, 4, 4, 5] Computed summation of powers : 110
Time Complexity: O(n*logn)
Auxiliary Space: O(n)
Method #3: Using Counter object + list comprehension
- Import Counter object from the collections module.
- Initialize the list.
- Get the frequency of each element using the Counter() method and store it in a dictionary.
- Compute the summation using a list comprehension that takes the power of the keys of the dictionary raised to the frequency of each element.
- Print the result.
Python3
| fromcollections importCounter# initializing listtest_list =[2, 2, 2, 3, 3, 3, 3, 4, 4, 5]# printing original listsprint("The original list is : "+str(test_list))# getting frequencytemp =Counter(test_list)# computing summationres =sum([key **temp[key] forkey intemp])# printing resultprint("Computed summation of powers : "+str(res)) | 
The original list is : [2, 2, 2, 3, 3, 3, 3, 4, 4, 5] Computed summation of powers : 110
Time complexity: O(n)
Auxiliary Space: O(n)
 
				 
					


