Skip to content
dreamcode
dreamcode
Projects
Packing Planner
Independent projectPython
Reward: +360 XP
PROJECT BRIEF

Packing Planner

A balloon basket can only carry so much. Each item is a dictionary like {"name": "tent", "weight": 4, "value": 9}. Write plan_packing(items, capacity) using this greedy plan:

  1. Rank items by value per unit of weight, best first. Items with the same ratio keep their original order.
  2. Walk down the ranking and pack every item that still fits in the remaining capacity. Skip the ones that do not.

Return {"packed": [...], "weight": total_weight, "value": total_value}, with packed listing names in the order you packed them.

Examples
plan_packing([{'name': 'tent', 'weight': 4, 'value': 8}, {'name': 'map', 'weight': 1, 'value': 5}, {'name': 'stove', 'weight': 3, 'value': 3}, {'name': 'rope', 'weight': 2, 'value': 6}], 6)
{'packed': ['map', 'rope', 'stove'], 'weight': 6, 'value': 14}
plan_packing([{'name': 'anvil', 'weight': 50, 'value': 1}], 10)
{'packed': [], 'weight': 0, 'value': 0}
project.py
PYTHON
Saved as you type

Tests

0 of 4 passing
  • best ratio first, skips what cannot fit
    plan_packing([{'name': 'tent', 'weight': 4, 'value': 8}, {'name': 'map', 'weight': 1, 'value': 5}, {'name': 'stove', 'weight': 3, 'value': 3}, {'name': 'rope', 'weight': 2, 'value': 6}], 6)
    expected {'packed': ['map', 'rope', 'stove'], 'weight': 6, 'value': 14}
  • nothing fits
    plan_packing([{'name': 'anvil', 'weight': 50, 'value': 1}], 10)
    expected {'packed': [], 'weight': 0, 'value': 0}
  • no items
    plan_packing([], 5)
    expected {'packed': [], 'weight': 0, 'value': 0}
  • ties keep their order
    plan_packing([{'name': 'a', 'weight': 2, 'value': 4}, {'name': 'b', 'weight': 1, 'value': 2}, {'name': 'c', 'weight': 3, 'value': 6}], 3)
    expected {'packed': ['a', 'b'], 'weight': 3, 'value': 6}
On the line
+360 XP
Pass all 4 tests to claim it.