Python | Arbitrary List Product
Sometimes, in making programs for gaming or gambling, we come across the task of creating a list with arbitrary numbers and performing its product. This task has to perform in general using a loop and appending the arbitrary numbers one by one and then performing the product. But there is always a requirement to perform this in most concise manner. Let us discuss certain ways in which this can be done.
Method #1 : Using list comprehension + randrange() + loop
The naive method to perform this particular task can be shortened using list comprehension. randrange function is used to perform the task of generating random numbers. The task of performing product is done using loop.
Python3
import random
def prod(val):
res = 1
for ele in val:
res * = ele
return res
res = prod([random.randrange( 1 , 50 , 1 ) for i in range ( 7 )])
print ( "Arbitrary number product list is : " + str (res))
|
Output :
Arbitrary number product list is : 1182384000
Method #2: Using random.sample() + loop
This single utility function performs the exact required as asked by the problem statement, it generated N no. of arbitrary numbers in a list in the specified range and returns the required list. The task of performing the product is done using loop.
Example:
Python3
import random
def prod(val):
res = 1
for ele in val:
res * = ele
return res
res = prod(random.sample( range ( 1 , 50 ), 7 ))
print ( "Arbitrary number product list is : " + str (res))
|
Output :
Arbitrary number product list is : 1182384000
Method #3: Using Reduce()
This code defines a function arbitrary_list_product() that takes a list lst as its argument and returns the product of all elements in the list. The function uses the reduce() function from the functools module to apply a lambda function that multiplies two elements to each pair of elements in the list, resulting in a single value that represents the product of all elements. This product is then returned as the output of the function.
Python3
from functools import reduce
def arbitrary_list_product_reduce(lst):
return reduce ( lambda x, y: x * y, lst)
my_list = [ 1 , 2 , 3 , 4 , 5 ]
product_reduce = arbitrary_list_product_reduce(my_list)
print (product_reduce)
|
Time complexity: O(n)
Auxiliary Space: O(1)
Method #4: using NumPy
Algorithm:
- Accept a list of integers as input.
- Call the np.prod() function from the NumPy library, passing in the input list as an argument.
- Return the result of the np.prod() function.
Python3
import numpy as np
def product_numpy(lst):
return np.prod(lst)
my_list = [ 1 , 2 , 3 , 4 , 5 ]
result = product_numpy(my_list)
print (result)
|
Output:
120
Time complexity:
The np.prod() function used in this code has a time complexity of O(n), where n is the number of elements in the input list. This is because it needs to iterate over each element in the list to calculate the product.
The product_numpy() function itself has a time complexity of O(1), as it simply calls the np.prod() function with the input list.
Auxiliary Space:
The product_numpy() function has a space complexity of O(1), as it only uses a small amount of memory to store the input list and the result of the np.prod() function. The np.prod() function also has a space complexity of O(1), as it doesn’t create any new data structures or use any additional memory beyond the input list.
Method #5: Using recursion to calculate the product of a list of numbers
Python3
def arbitrary_list_product_recursion(lst):
if not lst:
return 1
return lst[ 0 ] * arbitrary_list_product_recursion(lst[ 1 :])
my_list = [ 1 , 2 , 3 , 4 , 5 ]
product_recursion = arbitrary_list_product_recursion(my_list)
print (product_recursion)
|
Time complexity: O(n), where n is the length of the list.
Auxiliary space: O(n), due to the recursive function calls on the stack.
Last Updated :
25 Apr, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...