Command Palette

Search for a command to run...

Hectal
Phase 1: Search Before Vectors
RAG and Vector Search · Exercise 2 of 5 PreviousNext
Beginner

Build an inverted index and answer AND queries

docs is a dict of passage id to text, and tokenize(text) is given. Write build_index(docs) returning a dict from term to the list of passage ids that contain it (each id once, in the order of docs).

Write match_all(index, words) returning the list of ids that contain every word, in the order of the first word's postings. A word missing from the index means no passage matches: return [].

What Check tests

  1. Postings list the passages that contain a term
  2. A repeated word lists the passage once
  3. AND keeps passages with every word
  4. A missing word gives no match

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.