Python being the language of magicians can be used to perform many tedious and repetitive tasks in a easy and concise manner and having the knowledge to utilize this tool to the fullest is always useful. One such small application can be finding sum of i^k of list in just one line. Let’s discuss certain ways in which this can be performed.
Method #1 : Using reduce() + lambda + pow() The power of lambda functions to perform lengthy tasks in just one line, allows it combined with reduce which is used to accumulate the subproblem, to perform this task as well. The pow() is used to perform task of computing power. Works with only Python 2.
Python3
# Python code to demonstrate # i ^ k Summation in list # using reduce() + lambda + pow() # initializing list test_list = [ 3 , 5 , 7 , 9 , 11 ] # printing original list print ("The original list is : " + str (test_list)) # initializing K K = 4 # using reduce() + lambda + pow() # i ^ k Summation in list res = reduce ( lambda i, j: i + pow (j, K), [ pow (test_list[: 1 ][ 0 ], K)] + test_list[ 1 :]) # printing result print ("The sum of i ^ k of list is : " + str (res)) |
The original list is : [3, 5, 7, 9, 11] The sum of i^k of list is : 24309
Time complexity: O(n*n), where n is the length of the test_list. The reduce() + lambda + pow() takes O(n*n) time
Auxiliary Space: O(n), extra space of size n is required
Method #2 : Using map() + sum() + pow() The similar solution can also be obtained using the map function to integrate and sum function to perform the summation of the i^k number.
Python3
# Python3 code to demonstrate # i ^ k Summation in list # using map() + sum() + pow() # initializing list test_list = [ 3 , 5 , 7 , 9 , 11 ] # printing original list print ("The original list is : " + str (test_list)) # initializing K K = 4 # using map() + sum() + pow() # i ^ k Summation in list res = sum ( map ( lambda i : pow (i, K), test_list)) # printing result print ("The sum of i ^ k of list is : " + str (res)) |
The original list is : [3, 5, 7, 9, 11] The sum of i^k of list is : 24309
Time Complexity: O(n*n), where n is the length of the input list. This is because we’re using map() + sum() + pow() which has a time complexity of O(n*n) in the worst case.
Auxiliary Space: O(1), as we’re using constant additional space
Method #4 : Using Numpy
Note: Install numpy module using command “pip install numpy”
Python3
import numpy as np # initializing list test_list = [ 3 , 5 , 7 , 9 , 11 ] # printing original list print ( "The original list is : " + str (test_list)) # initializing K K = 4 # Using Numpy # i ^ k Summation in list res = np. sum (np.power(test_list, K)) # printing result print ( "The sum of i ^ k of list is : " + str (res)) #This code is contributed by Edula Vinay Kumar Reddy |
Output:
The original list is : [3, 5, 7, 9, 11]
The sum of i ^ k of list is : 24309
This method uses the numpy module to perform the computation. The np.power() function is used to compute the power of each element in the input list, and the np.sum() function is used to add up the results.
Time Complexity: O(n)
Space Complexity: O(n)
Method #4 : Using a for loop:
Python3
#initializing list test_list = [ 3 , 5 , 7 , 9 , 11 ] # printing original list print ( "The original list is : " + str (test_list)) #initializing K K = 4 res = 0 for i in test_list: res + = pow (i, K) # printing result print ( "The sum of i ^ k of list is : " + str (res)) #This code is contributed by pinjala Jyothi. |
The original list is : [3, 5, 7, 9, 11] The sum of i ^ k of list is : 24309
Time Complexity: O(n)
Space Complexity: O(1)
Method#5: Using Recursive method.
Algorithm:
- Define a function sum_of_powers(test_list, K) that takes a list test_list and an integer K as inputs.
- If test_list is empty, return 0 as the base case.
- Otherwise, compute the sum of the first element of test_list raised to the power of K, and the sum of the rest of the
- elements of test_list computed recursively using the same function sum_of_powers with the tail of test_list.
- Return the sum computed in step 3 as the final result.
Python3
# Python program for the above approach # Function to find the sum of powers def sum_of_powers(test_list, K): if len (test_list) = = 0 : return 0 else : return pow (test_list[ 0 ], K) + sum_of_powers(test_list[ 1 :], K) # Driver Code test_list = [ 3 , 5 , 7 , 9 , 11 ] K = 4 print ( "The original list is : " + str (test_list)) res = sum_of_powers(test_list, K) print ( "The sum of i ^ k of list is : " + str (res)) |
The original list is : [3, 5, 7, 9, 11] The sum of i ^ k of list is : 24309
Time complexity:
The time complexity of this recursive implementation of the code is O(n), where n is the length of the input list test_list.
Space complexity:
The space complexity of the recursive implementation is O(n) as well, where n is the length of the input list test_list.