FreeCampus Python

Milestone Challenge: Route Supplies through a Puzzle Village

Combine collections, decisions, loops, functions, and copying to allocate limited supplies.
Open in Colab
  • Goal: Combine collections, decisions, loops, functions, and copying to allocate limited supplies.
  • Estimated time: 2–4 hours of active work.
  • Before starting: Complete the milestone units and their unit challenges.
  • Keep: Your source, observed outputs, checks, and a debugging note.

This community challenge is ungraded. Use the checks to review your own work; there is no submission service or automatic grade. Completion is self-reported in this browser on this device.

1. Route supplies through a puzzle village

Implement allocate(stock, requests). stock maps names to nonnegative integer quantities. Each request is an (item, quantity) pair with a positive integer quantity. Process requests in order. Accept only when the full quantity is available; otherwise reject without changing stock. Unknown items are rejected. Return (remaining, decisions): a new stock dictionary and a list of booleans. Do not mutate either input. Treat valid quantities as preconditions.

Predict an ordinary result before coding. Start with the smallest part you can run and inspect. Use Colab for notebooks and the local workspace introduced in the units for packaging or command-line tasks.

2. Set up the experiment

def allocate(stock, requests):
    """Return independent remaining stock and one decision per request."""
    # Implement the allocation contract.
    pass

The starter is deliberately incomplete. Implement the contract; do not change the expected results to match an incorrect implementation.

3. Check the observable results

  1. For {“rope”: 3, “lamp”: 1} and [(“rope”, 2), (“rope”, 2), (“lamp”, 1)], return ({“rope”: 1, “lamp”: 0}, [True, False, True]).
  2. Empty requests return an equal but independent stock dictionary and an empty decisions list.
  3. An unknown item is rejected without adding a stock entry.
  4. After the call, the original stock is unchanged. Mutating remaining must not alter it. Explain why a shallow copy is sufficient for integer values.

Record actual outputs beside predictions. Reduce a failure to a small example, change one cause at a time, and repeat the failed check after repairing it.

4. Change one condition

Change one rope request to quantity 1, reverse the two rope requests, and trace the decisions. Explain why order is part of the contract.

5. Review a hint after attempting the task

Hint: connect the relevant tools

Begin with stock.copy(). Compare each request with remaining.get(item, 0), append its decision, and subtract only on acceptance. Return after processing every request.

6. Explain and self-review

Keep a trace table, assertions for ordinary and boundary cases, and an explanation of one aliasing bug. Show exactly which object changes at each step.

Review path: compare your result with the contract

Check the ordinary result first, then the boundary cases and changed condition. Explain which rule justifies each result. An implementation that differs from the hint can still satisfy the contract; explain why yours does.

7. Save your work and record completion

Save the notebook or project somewhere you control. Include one decision, one failed check, its repair, and a result you can reproduce. Record completion after reviewing the checks; you can undo it later.

Not yet recorded.

Return to the milestone overview.

Back to top