Command Palette

Search for a command to run...

Hectal
Phase 8: Retrieval Basics (RAG)
LLM Engineering · Exercise 1 of 6Next
Beginner

Build an inverted index

Write build_index(docs): docs is a dictionary from document name to text. Return a dictionary from each word to the sorted list of names of the documents that contain it (a document counts once per word, however often it repeats it).

Write search_all(index, query) that returns the sorted names of documents containing every word of the query. Words that no document has make the result empty, and an empty query returns []. Use the tokenize function given.

What Check tests

  1. index["days"] is ["returns", "shipping"] (sorted)
  2. A word repeated in one document lists it once
  3. search_all(index, "unused days") is ["returns"]
  4. A word nobody has gives []
  5. An empty query gives []

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.