Tous les défis
Facile 2 résolus 25.0 % de réussite

Cart Total With Discount

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

Ce que vous allez apprendre

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

Problème

Afficher la traduction

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.

Contraintes

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

Exemples

Exemple 1
Entrée :
prices = [40, 35, 25]
discount_threshold = 100
discount_percent = 10

Sortie attendue :
expected = 90.0
Entrée :
std::vector<long long> prices = {40, 35, 25};
long long discount_threshold = 100;
long long discount_percent = 10;

Sortie attendue :
double expected = 90.0;
Entrée :
long[] prices = new long[] {40, 35, 25};
long discount_threshold = 100;
long discount_percent = 10;

Sortie attendue :
double expected = 90.0;

Explication

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

Exemple 2
Entrée :
prices = [20, 30]
discount_threshold = 100
discount_percent = 10

Sortie attendue :
expected = 50.0
Entrée :
std::vector<long long> prices = {20, 30};
long long discount_threshold = 100;
long long discount_percent = 10;

Sortie attendue :
double expected = 50.0;
Entrée :
long[] prices = new long[] {20, 30};
long discount_threshold = 100;
long discount_percent = 10;

Sortie attendue :
double expected = 50.0;

Explication

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

Comment l'aborder

Essayez d'abord seul, puis ouvrez les indices un par un.

Indice 1

First find the cart total before any discount.

Indice 2

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

Indice 3

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

Complexité attendue

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

Références

Ce défi a été adapté pour CodeUtility. Suivez l'énoncé et les contraintes ci-dessus.

La meilleure solution validée de chaque codeur. Les plus rapides, les plus légères et les mieux notées passent devant.

# Codeur Langage Temps Mémoire Note de l'IA Solution
1 LMLaura Mahlow Python 823 ms - 3/5 Voir le code
2 HJhettory Jurash Python 828 ms - 3/5 Voir le code

Apprenez des autres

Solutions de la communauté

Attention spoiler : 2 solutions complètes ci-dessous Essayez d'abord de le résoudre vous-même. Vous pouvez toujours jeter un œil au code des autres développeurs et aux avis IA. Connectez-vous et résolvez le défi pour rejoindre la discussion.