AI Agents by HandAll problemsAbout

All problems › Python drill · medium · ~6 min

d17 · Select the best snippet

chunking retrieval

Solve it in your browser →

d17 · Select the best snippet (~6 min)

Chunking text. Edit drills.py. Standard library only; no model calls, no API key.

def best_snippet(body, query, max_chars=300):

Pick the paragraph of body that best answers query, for a search result.

Score each paragraph (as d15 splits them) by how many distinct query terms appear in it as whole words, ignoring case and surrounding punctuation (the given match_terms does this). Highest score wins; ties go to the earliest paragraph; if nothing matches, use the first. Return it shortened as in d16. No paragraphs -> "".

Given, already working in drills.py: match_terms (d10), split_paragraphs (d15), truncate_words (d16). Use them; they aren't part of this drill.

Failure messages are enough to go on, so leave the tests unread until you submit.

Starter code and hidden tests open in the editor. Nothing to install.