Cart Total with reduce()

Medium ⏱ 12 min 63% acceptance ★★★★★ 4.8
Write cart_total(prices) that computes the sum of a list of prices using functools.reduce and a lambda — no sum() allowed. An empty cart must return 0 (use the initializer argument).

Examples

Example 1
Input
cart_total([99, 1, 250])
Output
350
Explanation

reduce folds the list pairwise: ((99+1)+250).

Constraints

  • Import reduce from functools.
  • Empty list returns 0 — pass an initializer.

Topics

Lambda & Functionalreduce

Companies

PayPalRazorpayWalmart

Hints

Hint 1

reduce(f, seq, initial).

Hint 2

lambda acc, x: acc + x.

Loading the Python runtime… Run executes your code and shows printed output; Submit checks your function against this problem's examples.