Alle Herausforderungen
Einfach 0 gelöst 0% Erfolgsquote

Shipping Cost Function

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

Das lernst du

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

Problem

Übersetzung anzeigen

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.

Einschränkungen

−10,000 ≤ weight_ounces ≤ 10,000

Beispiele

Beispiel 1
Eingabe:
weight_ounces = 30
express = False

Erwartete Ausgabe:
expected = 750
Eingabe:
long long weight_ounces = 30;
bool express = false;

Erwartete Ausgabe:
long long expected = 750;
Eingabe:
long weight_ounces = 30;
bool express = false;

Erwartete Ausgabe:
long expected = 750;

Erläuterung

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

Beispiel 2
Eingabe:
weight_ounces = 10
express = True

Erwartete Ausgabe:
expected = 1000
Eingabe:
long long weight_ounces = 10;
bool express = true;

Erwartete Ausgabe:
long long expected = 1000;
Eingabe:
long weight_ounces = 10;
bool express = true;

Erwartete Ausgabe:
long expected = 1000;

Erläuterung

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

So gehst du vor

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

Hinweis 1

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

Hinweis 2

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

Hinweis 3

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

Erwartete Komplexität

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

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.

Noch keine bestandene Lösung. Löse sie und hol dir Platz eins.

Von anderen lernen

Community-Lösungen

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