Python | Triple Product to K
Last Updated :
09 Apr, 2023
The problem of getting the product number of pairs that lead to a particular solution has been dealt many times, this articles aims at extending that to 3 numbers and discussing several ways in which this particular problem can be solved. Let’s discuss certain ways in which this can be performed.
Method #1 : Using Nested loops This is the naive method in which this particular problem can be solved and takes outer loop to iterate for each elements and inner loop checks for the remaining difference multiplying the pairs to the result.
Python3
# Python3 code to demonstrate
# 3 element product
# using nested loops
# initializing list
test_list = [4, 1, 3, 2, 6, 12]
# initializing product
product = 24
# printing original list
print("The original list : " + str(test_list))
# using nested loops
# 3 element product
res = []
for i in range(0, len(test_list)-2):
for j in range(i + 1, len(test_list)-1):
for k in range(j + 1, len(test_list)):
if test_list[i] * test_list[j] * test_list[k] == product:
temp = []
temp.append(test_list[i])
temp.append(test_list[j])
temp.append(test_list[k])
res.append(tuple(temp))
# print result
print("The 3 product element list is : " + str(res))
Output : The original list : [4, 1, 3, 2, 6, 12]
The 3 product element list is : [(4, 1, 6), (4, 3, 2), (1, 2, 12)]
Time Complexity: O(n*n*n) where n is the number of elements in the list “test_list”.
Auxiliary Space: O(n) where n is the number of elements in the list “test_list”.
Method #2 : Using itertools.combination() This particular problem can also be done in a concise manner using the inbuilt function of function. The combination function finds all the combination taking K arguments leading to particular product.
Python3
# Python3 code to demonstrate
# Triple Product to K
# using itertools.combination()
from itertools import combinations
# function to get the product
def test(val):
prod = 1
for ele in val:
prod *= ele
return (prod == 24)
# initializing list
test_list = [4, 1, 3, 2, 6, 12]
# initializing product
product = 24
# printing original list
print("The original list : " + str(test_list))
# using itertools.combination()
# 3 element product
res = list(filter(test, list(combinations(test_list, 3))))
# print result
print("The 3 product element list is : " + str(res))
Output : The original list : [4, 1, 3, 2, 6, 12]
The 3 product element list is : [(4, 1, 6), (4, 3, 2), (1, 2, 12)]
Time Complexity: O(n*n), where n is the number of elements in the list “test_list”.
Auxiliary Space: O(n*n), where n is the number of elements in the list “test_list”.
Similar Reads
Python - String Integer Product Sometimes, while working with data, we can have a problem in which we receive a series of lists with data in string format, which we wish to find the product of each string list integer. Letâs discuss certain ways in which this task can be performed. Method #1: Using loop + int()This is the brute fo
4 min read
Python | K Value Indices Product Usually, we require to find the index, in which the particular value is located. There are many method to achieve that, using index() etc. But sometimes require to find all the indices of a particular value in case it has multiple occurrences in list and compute their product. Lets discuss certain w
7 min read
Python - Elements Product till K value One of the problem that is basically a subproblem for many complex problems, finding product number till a certain number in list in python, is commonly encountered and this particular article discusses possible solutions to this particular problem. Method #1 : Naive method The most common way this
5 min read
Python | Record Index Product Sometimes, while working with records, we can have a problem in which we need to multiply all the columns of a container of lists that are tuples. This kind of application is common in web development domain. Letâs discuss certain ways in which this task can be performed. Method #1 : Using loop + li
4 min read
Python - Filter Tuples Product greater than K Given a Tuple list, extract all with product greater than K. Input : test_list = [(4, 5, 7), (1, 2, 3), (8, 4, 2), (2, 3, 4)], K = 50Â Output : [(4, 5, 7), (8, 4, 2)]Â Explanation : 140 and 64 are greater than 50, hence tuples extracted.Input : test_list = [(4, 5, 7), (1, 2, 3), (8, 4, 2), (2, 3, 4)
7 min read