모든 챌린지
쉬움 0명 해결 정답률 0%

Shipping Cost Function

Complete the function that prices a package from its weight in ounces and the delivery speed.

배우게 될 내용

  • Validate inputs before computing
  • Count started blocks with ceiling division
  • Apply a multiplier based on a boolean

문제

번역본 보기

Complete the function for an online craft store that returns the shipping price of a package in cents. The store's rules are:

  • Packages up to 16 ounces cost 500 cents.
  • Above 16 ounces, add 125 cents for every started 8 ounces beyond the first 16 (17 to 24 ounces adds 125, 25 to 32 adds 250, and so on).
  • Express delivery doubles the whole price.
  • The carrier only takes packages from 1 ounce up to 1120 ounces (70 pounds).

Implement shipping_cost_function(weight_ounces, express).

Input

weight_ounces: The weight of the package in whole ounces.

express: true for express delivery, false for standard.

Output

Return the price in cents. If the weight is below 1 or above 1120, return -1.

제약 조건

−10,000 ≤ weight_ounces ≤ 10,000

예시

예시 1
입력:
weight_ounces = 30
express = False

예상 출력:
expected = 750
입력:
long long weight_ounces = 30;
bool express = false;

예상 출력:
long long expected = 750;
입력:
long weight_ounces = 30;
bool express = false;

예상 출력:
long expected = 750;

설명

30 ounces is 14 ounces over 16, which starts 2 blocks of 8 ounces: 500 + 2 × 125 = 750 cents.

예시 2
입력:
weight_ounces = 10
express = True

예상 출력:
expected = 1000
입력:
long long weight_ounces = 10;
bool express = true;

예상 출력:
long long expected = 1000;
입력:
long weight_ounces = 10;
bool express = true;

예상 출력:
long expected = 1000;

설명

10 ounces costs the base 500 cents, doubled for express: 1000.

접근 방법

먼저 스스로 풀어 본 뒤 힌트를 하나씩 열어 보세요.

힌트 1

Return -1 right away for weights outside 1 to 1120.

힌트 2

Started blocks of 8 over 16 ounces is (extra + 7) / 8 with integer division.

힌트 3

Double the final total, not just the extra charge, for express.

예상 복잡도

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

참고 자료

이 문제는 CodeUtility에 맞게 수정되었습니다. 위의 설명과 제한 조건을 따르세요.

코더마다 가장 좋은 통과 풀이입니다. 더 빠르고, 메모리를 덜 쓰고, 평가가 좋은 풀이가 위에 옵니다.

아직 통과한 풀이가 없습니다. 풀어서 1위를 차지하세요.

다른 사람의 풀이 보기

커뮤니티 솔루션

아직 공개된 풀이가 없습니다. 문제를 풀고 첫 풀이를 공유하세요.