MediumPython

functools.reduce — Running Product

PythonFunctionalfunctools

Implement solve(numbers) using only functools.reduce (no loops) that returns the product of all elements in numbers.

If numbers is empty, return 1 (identity element for multiplication).

Examples

  • solve([1, 2, 3, 4])24
  • solve([5])5
  • solve([])1
  • solve([2, 0, 5])0

Constraints

  • Must use functools.reduce.
  • No explicit for/while loops.

Sample tests

Test #1Product of 1-4
Input: [[1,2,3,4]]
Output: 24
Test #2Single element
Input: [[5]]
Output: 5
Test #3Empty list returns identity 1
Input: [[]]
Output: 1
Test #4Zero in list
Input: [[2,0,5]]
Output: 0