A shipping program accepts valid parcel dictionaries. A Local parcel contains kind="local" and a positive weight_kg. An International parcel contains kind="international", a positive weight_kg, and a declaration dictionary. That declaration is either Documents, with only kind="documents", or Goods, with kind="goods" and a nonnegative value_dollars.
Local shipping costs 5.00 dollars per kilogram. International shipping costs 12.00 dollars plus 7.00 dollars per kilogram, with an additional 4% of declared value for Goods only. The following function fails for some valid inputs. Which correction makes it implement the stated charges for every permitted form?
def charge(parcel): if parcel["kind"] == "local": return 5.00 * parcel["weight_kg"] declaration = parcel["declaration"] return (12.00 + 7.00 * parcel["weight_kg"] + 0.04 * declaration["value_dollars"])
Check the declaration's kind within International and include the value-based term only when that declaration is Goods.
Remove the value-based term from every International calculation, leaving the existing Local calculation unchanged.
Add a zero-valued value_dollars field to Local parcels, leaving the existing international calculation unchanged.
Assign zero to declaration["value_dollars"] for every International parcel before performing the existing calculation.
Unlock full course notes by signing up
Decide which fields require a case check
A meal-order program uses dictionaries with a kind entry. Its data definition permits Counter(item_count) and Delivered(item_count, distance_km). Every valid value contains a positive integer item count; only Delivered values contain a nonnegative delivery distance. Assuming inputs satisfy this definition, which statements about function structure are true? Select all that apply.
Complete a recursive counting template
A temperature logger represents its readings using the following data definition:
Readings is either Empty or Link(value, next).
value is an integer temperature in degrees Celsius.
Count the values a definition permits
An access-control program uses the following finite data definitions:
Grant has two fields: level, either viewer or editor; and expiry, either one day, one week, or one month.
Decision is either Deny, which has no fields, or Allow(grant), which contains one Grant.
Trace calls across nested data definitions
A file browser uses two data definitions:
Item is either File(size) or Folder(contents). File sizes are non-negative integers in bytes, and folder contents are an ItemList.
ItemList is either Empty() or Cons(first, rest), where first is an Item and rest is another ItemList.