MediumPro challengePythonJavaScriptTypeScript
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])→24solve([5])→5solve([])→1solve([2, 0, 5])→0
Constraints
- Must use
functools.reduce. - No explicit for/while loops.
In JavaScript / TypeScript — Use Array.prototype.reduce with an initial value of 1 — no 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