Alle Herausforderungen
Einfach 2 gelöst 25.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_threshold = 100
discount_percent = 10

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

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

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_threshold = 100
discount_percent = 10

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

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

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.

Die beste bestandene Lösung jedes Coders. Schnellere, sparsamere und besser bewertete Lösungen stehen weiter oben.

# Coder Sprache Zeit Speicher KI-Bewertung Lösung
1 LMLaura Mahlow Python 823 ms - 3/5 Code ansehen
2 HJhettory Jurash Python 828 ms - 3/5 Code ansehen

Von anderen lernen

Community-Lösungen

Achtung Spoiler: 2 vollständige Lösungen unten Versuche es zuerst selbst zu lösen. Du kannst trotzdem einen Blick auf den Code und die KI-Reviews anderer Entwickler werfen. Melde dich an und löse die Aufgabe, um an der Diskussion teilzunehmen.