Compare Two Shopping Cart Totals

Easy ⏱ 8 min 73% acceptance ★★★★★ 4.9
Each shopping cart is a list of (item_name, price, quantity) tuples. Write compare_carts(cart_a, cart_b) that returns a string: "cart_a" if cart_a's total (sum of price*quantity across items) is strictly higher, "cart_b" if cart_b's is strictly higher, or "tie" if they are equal.

Examples

Example 1
Input
cart_a = [('Pen', 10, 2)], cart_b = [('Book', 15, 1)]
Output
'cart_a'
Explanation

cart_a total = 10*2 = 20; cart_b total = 15*1 = 15; 20 > 15 so cart_a wins.

Example 2
Input
cart_a = [('Pen', 10, 2)], cart_b = [('Book', 20, 1)]
Output
'tie'
Explanation

Both totals equal 20.

Constraints

  • 0 <= len(cart_a), len(cart_b) <= 10^4

Topics

ListsBusiness Logic

Companies

FlipkartAmazon

Hints

Hint 1

Compute each cart's total with sum(price * qty for _, price, qty in cart).

Hint 2

Compare the two totals and return the matching label.

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