Command Palette

Search for a command to run...

Hectal
Phase 0: Start Here: What an LLM Is and Why Engineering Matters
LLM Engineering · Exercise 2 of 5 PreviousNext
Beginner

Generate greedily, with a stop rule

table maps a word to a dictionary of next-word counts. Write generate(table, start, max_tokens) that returns a list of words starting with start.

Each step adds the most frequent next word of the last word; break ties by taking the alphabetically first word. Stop when the list has max_tokens words, when the last word is ".", or when the last word has no next words in the table.

What Check tests

  1. Picks the most frequent word and breaks ties alphabetically
  2. Stops at max_tokens words
  3. Stops when the last word has no next words
  4. max_tokens of 1 returns just the start

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.