Plan a Map-Reduce Summarisation

medium
Unit 24 · Calibration0/3 to milestone

To summarise a document far bigger than the context window: summarise groups of chunks (map), then summarise the summaries (reduce), repeating until one call can see everything.

Implement plan(chunk_tokens, context_limit, summary_tokens, overhead):

  • Each call's input is its items' tokens plus overhead (instructions), and must fit in context_limit.
  • Group items greedily in order: keep adding the next item while the call still fits. A single item that can't fit raises ValueError.
  • Each call outputs one summary of summary_tokens tokens.
  • Level 0 groups the chunks. Each later level groups the previous level's summaries. Stop after a level with a single call.
  • If summary_tokens is so large that a level doesn't shrink the number of items, raise ValueError (it would never finish).

Return a list of levels, each a list of group sizes (items per call). Also implement total_calls(levels).

#long-context#summarization#planning2 visible + 2 hidden tests
solution.py · Python 3.13
Loading editor…
Run your code to see results. ⌘↵ to run · ⌘⇧↵ to submit
Plan a Map-Reduce Summarisation · TechGradify