簡単 2件解決済み 正答率25.0%

Cart Total With Discount

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

このチャレンジで学べること

  • 数量を考慮して価格を合計する
  • しきい値を超えたら割引を適用する
  • コードを補助関数に整理する

問題

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.

制約

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

例

例 1
入力:
prices = [40, 35, 25]
discount_threshold = 100
discount_percent = 10

期待される出力:
expected = 90.0
入力:
std::vector<long long> prices = {40, 35, 25};
long long discount_threshold = 100;
long long discount_percent = 10;

期待される出力:
double expected = 90.0;
入力:
long[] prices = new long[] {40, 35, 25};
long discount_threshold = 100;
long discount_percent = 10;

期待される出力:
double expected = 90.0;

解説

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

例 2
入力:
prices = [20, 30]
discount_threshold = 100
discount_percent = 10

期待される出力:
expected = 50.0
入力:
std::vector<long long> prices = {20, 30};
long long discount_threshold = 100;
long long discount_percent = 10;

期待される出力:
double expected = 50.0;
入力:
long[] prices = new long[] {20, 30};
long discount_threshold = 100;
long discount_percent = 10;

期待される出力:
double expected = 50.0;

解説

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

解き方のヒント

まずは自分で考えてから、ヒントを 1 つずつ開いてみましょう。

ヒント 1

まず、割引前のカート合計を求めます。

ヒント 2

合計がしきい値に達したときだけ、割引率を適用します。

ヒント 3

割引は各商品の合計ではなく、注文全体から引くことに注意してください。

目安の計算量

時間 O(n)、追加メモリ O(1)

参考資料

この課題はCodeUtility用に調整されています。上記の説明と制約に従ってください。

ランキング

全体ランキング

コーダーごとのいちばん良い合格解答です。速く、メモリが少なく、評価が高い解答ほど上位になります。

# コーダー 言語 時間 メモリ AI 評価 解答
1 LMLaura Mahlow Python 823 ms - 3/5 コードを見る
2 HJhettory Jurash Python 828 ms - 3/5 コードを見る

他の人の解答から学ぶ

コミュニティの解答

ネタバレ注意:下に2件の全解答あり まずは自分で挑戦してみましょう。他の開発者のコードやAIレビューも見ることができます。 サインインして課題を解くと議論に参加できます。