Some of the classical problems in the programming domain come from different categories and one among them is finding the product of subsets. This particular problem is also common when we need to compute the product and store consecutive group product values. Let’s try different approaches to this problem in Python language.
Method #1 : Using list comprehension + loop
The list comprehension can be used to perform this particular task to filter out successive groups and the product explicit function can be used to get the product of the filtered solution.
Python3
def prod(val):
res = 1
for ele in val:
res * = ele
return res
test_list = [ 4 , 7 , 8 , 10 , 12 , 15 , 13 , 17 , 14 ]
print ( "The original list : " + str (test_list))
res = [prod(test_list[x: x + 3 ]) for x in range ( 0 , len (test_list), 3 )]
print ( "The chunked product list is : " + str (res))
|
Output : The original list : [4, 7, 8, 10, 12, 15, 13, 17, 14]
The chunked product list is : [224, 1800, 3094]
Time Complexity: O(n)
Auxiliary Space: O(n)
Method #2: Using loop + itertools.islice()
The task of slicing the list into chunks is done by islice method here and the conventional task of getting the product is done by the explicit product function as the above method.
Python3
import itertools
def prod(val):
res = 1
for ele in val:
res * = ele
return res
test_list = [ 4 , 7 , 8 , 10 , 12 , 15 , 13 , 17 , 14 ]
print ( "The original list : " + str (test_list))
res = [prod( list (itertools.islice(test_list, i, i + 3 )))
for i in range ( 0 , len (test_list), 3 )]
print ( "The chunked product list is : " + str (res))
|
Output : The original list : [4, 7, 8, 10, 12, 15, 13, 17, 14]
The chunked product list is : [224, 1800, 3094]
Time Complexity: O(n) where n is the number of elements in the list “test_list”. loop + itertools.islice() performs n number of operations.
Auxiliary Space: O(n), extra space is required where n is the number of elements in the list
Method #3 : Using reduce() + lambda
The inbuilt reduce function and the lambda function can also be used to perform this particular task. The reduce function is used to perform the product operation over the chunks and lambda is used to filter out the chunks.
Python3
from functools import reduce
test_list = [ 4 , 7 , 8 , 10 , 12 , 15 , 13 , 17 , 14 ]
print ( "The original list : " + str (test_list))
res = [ reduce ( lambda x, y: x * y, test_list[x: x + 3 ])
for x in range ( 0 , len (test_list), 3 )]
print ( "The chunked product list is : " + str (res))
|
OutputThe original list : [4, 7, 8, 10, 12, 15, 13, 17, 14]
The chunked product list is : [224, 1800, 3094]
Time complexity: O(N)
Auxiliary Space: O(N)
Method #4: Using numpy.array_split() and numpy.prod()
Steps:
- Import numpy library.
- Initialize the input list.
- Split the input list into chunks of 3 using numpy.array_split() function.
- Calculate the product of each chunk using numpy.prod() function.
- Store the result in a list.
- Print the result.
Python3
import numpy as np
test_list = [ 4 , 7 , 8 , 10 , 12 , 15 , 13 , 17 , 14 ]
print ( "The original list : " + str (test_list))
res = [np.prod(chunk)
for chunk in np.array_split(test_list, len (test_list) / / 3 )]
print ( "The chunked product list is : " + str (res))
|
OutputThe original list : [4, 7, 8, 10, 12, 15, 13, 17, 14]
The chunked product list is : [224, 1800, 3094]
Time complexity: O(n), where n is the length of the input list.
Auxiliary space: O(n), where n is the length of the input list.