Command Palette

Search for a command to run...

Hectal
Phase 8: Retrieval Basics (RAG)
LLM Engineering · Exercise 4 of 6 PreviousNext
Intermediate

Chunk words with overlap

Write chunk_words(words, size, overlap) that returns a list of strings, each the joined words (with single spaces) of one window of at most size words. Each window starts size - overlap words after the previous one, and the last window ends at the last word.

If overlap >= size, raise ValueError. An empty list of words gives []. If all words fit in one window, return that one chunk.

What Check tests

  1. 46 words, size 12, overlap 0 give 4 chunks
  2. 46 words, size 12, overlap 4 give 6 chunks
  3. The second chunk starts 8 words after the first
  4. The last chunk reaches the last word
  5. Few words give one chunk, and no words give []
  6. overlap >= size raises ValueError

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.