Command Palette

Search for a command to run...

Hectal
Phase 4: Ingestion: From Files to Searchable Chunks
RAG and Vector Search · Exercise 3 of 6 PreviousNext
Intermediate

Chunk Markdown with heading paths

Write chunk_markdown(markdown) returning a list of (path, text) tuples, one for every heading that has text under it.

A line that starts with one to three # signs and a space is a heading. Its level is the number of # signs. The path joins the headings above it and itself with " > ", for example Guide > Safety: a heading of level 2 replaces everything from level 2 down in the path.

The text of a section is its non-empty lines, stripped, joined by single spaces. Headings with no text under them (before the next heading) give no tuple.

What Check tests

  1. Sections with text become chunks
  2. The path includes the parent heading
  3. Lines of a section are joined with single spaces
  4. A new heading of the same level replaces the previous one
  5. A document with no headings gives no chunks

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.