Alle Herausforderungen
Einfach 0 gelöst 0% Erfolgsquote

Cart Total With Discount

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

Das lernst du

  • Preise mit Mengen summieren
  • Ab einer Schwelle einen Rabatt anwenden
  • Code in Hilfsfunktionen gliedern

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.

Einschränkungen

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

Beispiele

Beispiel 1
Eingabe:
prices = [40, 35, 25]
discount_percent = 10
discount_threshold = 100

Erwartete Ausgabe:
expected = 90.0
Eingabe:
std::vector<long long> prices = {40, 35, 25};
long long discount_percent = 10;
long long discount_threshold = 100;

Erwartete Ausgabe:
double expected = 90.0;
Eingabe:
long[] prices = new long[] {40, 35, 25};
long discount_percent = 10;
long discount_threshold = 100;

Erwartete Ausgabe:
double expected = 90.0;

Erläuterung

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

Beispiel 2
Eingabe:
prices = [20, 30]
discount_percent = 10
discount_threshold = 100

Erwartete Ausgabe:
expected = 50.0
Eingabe:
std::vector<long long> prices = {20, 30};
long long discount_percent = 10;
long long discount_threshold = 100;

Erwartete Ausgabe:
double expected = 50.0;
Eingabe:
long[] prices = new long[] {20, 30};
long discount_percent = 10;
long discount_threshold = 100;

Erwartete Ausgabe:
double expected = 50.0;

Erläuterung

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

So gehst du vor

Versuch es zuerst allein und öffne dann einen Hinweis nach dem anderen.

Hinweis 1

Berechne zuerst den Gesamtwert des Warenkorbs vor jedem Rabatt.

Hinweis 2

Wende die prozentuale Ermäßigung nur an, wenn der Gesamtwert den Schwellenwert erreicht.

Hinweis 3

Der Rabatt muss auf die gesamte Bestellung gerechnet werden, nicht auf jedes Item einzeln.

Erwartete Komplexität

Zeit O(n), zusätzlicher Speicher O(1)

Referenzen

Diese Aufgabe wurde für CodeUtility angepasst. Beachte die oben genannten Vorgaben und Einschränkungen.

Von anderen lernen

Community-Lösungen

Noch keine veröffentlichten Lösungen. Löse die Aufgabe und teile als Erster eine Lösung.