question 150 Advanced Functions & Recursion · hard
What does a kit cost to build?
Runtime Market sells kits that are built from parts, and some parts are themselves built from smaller parts. A bought part (one with no parts of its own) costs its price from the price list. Building one unit of an assembly costs labour plus, for each line of its parts list, qty × the cost of that part. Write assembly_cost(part, prices, *, labour=0). labour must be passed by name.
Function
assembly_cost(part, prices, *, labour=0)part
{"name": …, "parts": [{"qty": n, "part": {…same shape…}}, …]}; an empty parts list means a bought partpricesa dictionary of bought-part name → price
Returnsthe cost of one unit of
part; bought parts never add labourExampleCamping Kit = 1 Studio Tent + 2 Lantern Sets; a Lantern Set = 2 LED Lanterns + 4 AA Batteries. With labour 3: Lantern Set 3 + 25 + 2 = 30, so the kit costs 3 + 89 + 2 × 30 = 152
Examples
callassembly_cost({'name': 'Camping Kit', 'parts': [{'qty': 1, 'part': {'name': 'Studio Tent', 'parts': []}}, {'qty': 2, 'part': {'name': 'Lantern Set', 'parts': [{'qty': 2, 'part': {'name': 'LED Lantern', 'parts': []}}, {'qty': 4, 'part': {'name': 'AA Battery', 'parts': []}}]}}]}, {'Studio Tent': 89, 'LED Lantern': 12.5, 'AA Battery': 0.5})returns143
callassembly_cost({'name': 'Camping Kit', 'parts': [{'qty': 1, 'part': {'name': 'Studio Tent', 'parts': []}}, {'qty': 2, 'part': {'name': 'Lantern Set', 'parts': [{'qty': 2, 'part': {'name': 'LED Lantern', 'parts': []}}, {'qty': 4, 'part': {'name': 'AA Battery', 'parts': []}}]}}]}, {'Studio Tent': 89, 'LED Lantern': 12.5, 'AA Battery': 0.5}, labour=3)returns152
callassembly_cost({'name': 'Lantern Set', 'parts': [{'qty': 2, 'part': {'name': 'LED Lantern', 'parts': []}}, {'qty': 4, 'part': {'name': 'AA Battery', 'parts': []}}]}, {'Studio Tent': 89, 'LED Lantern': 12.5, 'AA Battery': 0.5}, labour=3)returns30