Command Palette

Search for a command to run...

Hectal
Phase 8: Generation with Sources
RAG and Vector Search · Exercise 1 of 5Next
Beginner

Pack sources into a token budget

estimate_tokens(text) is given. Write pack(hits, budget, overhead=8). hits is a list of (doc_id, text) pairs, best first.

Walk down the list. A passage costs estimate_tokens(text) + overhead. Keep it if the total used so far plus its cost is at most budget. If it does not fit, skip it and try the next one. Return the list of kept ids in the original order.

What Check tests

  1. Passages are kept while they fit
  2. A passage that does not fit is skipped, a later small one is kept
  3. The overhead is charged
  4. The overhead can be changed
  5. An empty list gives an empty list

Stuck? One hint at a time

The solution unlocks after your first Check. Try first: that is where the learning happens.

main.py
Sign in to run your code.

Output

Press Run to see what your program prints.