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
- Passages are kept while they fit
- A passage that does not fit is skipped, a later small one is kept
- The overhead is charged
- The overhead can be changed
- 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.