Sunday, November 17, 2024
Google search engine
HomeLanguagesPython – Sum of Cubes in List

Python – Sum of Cubes in List

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 cubes of list in just one line. Let’s discuss certain ways in which this can be performed. 

Method #1 : Using reduce() + lambda 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. Works with only Python 2. 

Python




# Python code to demonstrate
# Cube Summation in List
# using reduce() + lambda
 
# initializing list
test_list = [3, 5, 7, 9, 11]
 
# printing original list
print ("The original list is : " + str(test_list))
 
# using reduce() + lambda
# Cube Summation in List
res = reduce(lambda i, j: i + j * j*j, [test_list[:1][0]**3]+test_list[1:])
 
# printing result
print ("The sum of cubes of list is : " + str(res))


Output

The original list is : [3, 5, 7, 9, 11]
The sum of cubes of list is : 2555

Time Complexity: O(n)
Auxiliary Space: O(1)
 Method #2 : Using map() + sum() The similar solution can also be obtained using the map function to integrate and sum function to perform the summation of the cube number. 

Python3




# Python3 code to demonstrate
# Cube Summation in List
# using sum() + map()
 
# initializing list
test_list = [3, 5, 7, 9, 11]
 
# printing original list
print ("The original list is : " + str(test_list))
 
# using sum() + map()
# Cube Summation in List
res = sum(map(lambda i : i * i * i, test_list))
 
# printing result
print ("The sum of cubes of list is : " + str(res))


Output

The original list is : [3, 5, 7, 9, 11]
The sum of cubes of list is : 2555

Time complexity: O(n), where n is the length of the input list.
Auxiliary space: O(1), constant extra space is required

Method #3 : Using math.pow() and sum() methods

Python3




# Python code to demonstrate
# Cube Summation in List
 
# initializing list
test_list = [3, 5, 7, 9, 11]
 
# printing original list
print ("The original list is : " + str(test_list))
 
 
# Cube Summation in List
x=[]
for i in test_list:
    import math
    x.append(math.pow(i,3))
res=int(sum(x))
     
# printing result
print ("The sum of cubes of list is : " + str(res))


Output

The original list is : [3, 5, 7, 9, 11]
The sum of cubes of list is : 2555

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

Method #4 : Using list comprehension:

Python3




# Initializing list
test_list = [3, 5, 7, 9, 11]
# Printing original list
print("The original list is :", test_list)
# Using list comprehension
res = sum([num**3 for num in test_list])
# Printing result
print("The sum of cubes of list is :", res)
#This code is contributed by Jyothi pinjala.


Output

The original list is : [3, 5, 7, 9, 11]
The sum of cubes of list is : 2555

Time Complexity: O(N)
Auxiliary Space:  O(1)

Method#5: Using for loop

Python3




def sum_of_cubes(lst):
    res = 0
    for num in lst:
        res += num**3
    return res
 
test_list = [3, 5, 7, 9, 11]
print("The original list is:", test_list)
print("The sum of cubes of list is:", sum_of_cubes(test_list))
#this code is contributed By Vinay pinjala.


Output

The original list is: [3, 5, 7, 9, 11]
The sum of cubes of list is: 2555

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