Легко Решено: 0 Процент решений: 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_percent = 10
discount_threshold = 100

Ожидаемый вывод:
expected = 90.0
Ввод:
std::vector<long long> prices = {40, 35, 25};
long long discount_percent = 10;
long long discount_threshold = 100;

Ожидаемый вывод:
double expected = 90.0;
Ввод:
long[] prices = new long[] {40, 35, 25};
long discount_percent = 10;
long discount_threshold = 100;

Ожидаемый вывод:
double expected = 90.0;

Пояснение

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

Пример 2
Ввод:
prices = [20, 30]
discount_percent = 10
discount_threshold = 100

Ожидаемый вывод:
expected = 50.0
Ввод:
std::vector<long long> prices = {20, 30};
long long discount_percent = 10;
long long discount_threshold = 100;

Ожидаемый вывод:
double expected = 50.0;
Ввод:
long[] prices = new long[] {20, 30};
long discount_percent = 10;
long discount_threshold = 100;

Ожидаемый вывод:
double expected = 50.0;

Пояснение

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

Как подступиться

Сначала попробуйте сами, затем открывайте подсказки по одной.

Подсказка 1

Сначала найди итоговую сумму корзины до скидки.

Подсказка 2

Применяй процентную скидку только если сумма достигает порога.

Подсказка 3

Следи, чтобы скидка считалась от всего заказа, а не по каждому товару отдельно.

Ожидаемая сложность

время O(n), доп. память O(1)

Ссылки

Это задание адаптировано для CodeUtility. Следуйте условию и ограничениям выше.

Учитесь у других

Решения сообщества

Пока нет опубликованных решений. Решите задание, чтобы поделиться первым.