Back to challenges
Easy 0 solved 0% acceptance

Cart Total With Discount

Total a shopping cart and apply a discount above a threshold.

What you'll learn

  • Total prices with quantities
  • Apply a discount above a threshold
  • Organize code into helper functions

Problem

Add up the prices in the cart. If the total is at least discount_threshold, take discount_percent percent off the whole total.

Implement cart_total(prices, discount_threshold, discount_percent).

Input

prices: The item prices. discount_threshold: The total that unlocks the discount. discount_percent: The discount in percent.

Output

Return the amount to pay.

Constraints

0 ≤ length ≤ 1,000; 0 ≤ each price ≤ 100,000; 0 ≤ discount_percent ≤ 100

Examples

Example 1
Input:
prices = [40, 35, 25]
discount_percent = 10
discount_threshold = 100

Expected output:
expected = 90.0
Input:
std::vector<long long> prices = {40, 35, 25};
long long discount_percent = 10;
long long discount_threshold = 100;

Expected output:
double expected = 90.0;
Input:
long[] prices = new long[] {40, 35, 25};
long discount_percent = 10;
long discount_threshold = 100;

Expected output:
double expected = 90.0;

Explanation

The total is 100, which reaches the threshold, so 10% off: 90.

Example 2
Input:
prices = [20, 30]
discount_percent = 10
discount_threshold = 100

Expected output:
expected = 50.0
Input:
std::vector<long long> prices = {20, 30};
long long discount_percent = 10;
long long discount_threshold = 100;

Expected output:
double expected = 50.0;
Input:
long[] prices = new long[] {20, 30};
long discount_percent = 10;
long discount_threshold = 100;

Expected output:
double expected = 50.0;

Explanation

The total 50 is below 100, so no discount: 50.

How to approach it

Try on your own first, then open one hint at a time.

Hint 1

First find the cart total before any discount.

Hint 2

Apply the percentage reduction only when the total reaches the threshold.

Hint 3

Make sure the discount is taken from the whole order, not item by item.

Expected complexity

O(n) time, O(1) extra space

References

This challenge is adapted for CodeUtility. Follow the statement and constraints above.

Learn from others

Community solutions

No published solutions yet. Solve this challenge to share the first one.