Pure Function to Total a Cart (No Side Effects)

Easy ⏱ 8 min 85% acceptance ★★★★★ 4.9
Write a pure function cart_total(items) that takes a list of (price, quantity) tuples and returns the total cost, without modifying the input list or any variable outside the function. Purity here means: same input always gives same output, and no external state is read or changed.

Examples

Example 1
Input
cart_total([(100, 2), (50, 3)])
Output
350
Explanation

100*2 + 50*3 = 200 + 150 = 350.

Example 2
Input
cart_total([])
Output
0
Explanation

An empty cart totals to 0.

Constraints

  • Do not mutate the items list passed in.
  • Do not read or write any global/module-level variable.

Topics

Functionspure functions

Companies

FlipkartZomato

Hints

Hint 1

A generator expression like sum(p * q for p, q in items) avoids building intermediate lists or touching outside state.

Hint 2

An empty list should sum to 0 automatically via sum().

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