The cart total
- O(n) · Medium
- Full plan
- Python
- JavaScript
- lists
- loops
Problem
A shopping cart keeps its data in two lists of the same length: prices has the price in dollars of each item, and counts how many of that same item you are buying. The position joins them: the price at position 0 goes with the count at position 0.
Write a function that takes the two lists and returns the total to pay: the sum of price times count for each item. With both lists empty the total is 0.
Examples
[10, 20], [2, 1] → 40
[5], [3] → 15
One item with a count of 0
[7, 3], [0, 4] → 12
Besides these, the challenge has hidden tests that are revealed when you submit your solution.
You start with this
Python
def cart_total(prices, counts):
passJavaScript
function cartTotal(prices, counts) {
}Solve this challenge
It opens in your browser, with the editor and the tests. This challenge is part of the full plan; the O(1) and O(log n) ones are free.