bugger.
All challenges/Pythonmediumllm-bpe-encoding-v1
THE INVESTIGATION

Encode and decode with byte-pair ranks

Encode UTF-8 bytes with ranked merges and reconstruct the text from token IDs.

LLM ArchitectureTokenization

What’s going wrong

Original LLM Architecture lab. Implement the missing behavior in solve(data) and keep the supplied JSON interface. Use Python’s standard library.

Input text is one Unicode string. Each merge [left,right,id] references byte IDs 0–255 or previously defined merged IDs, and the array order defines rank. Repeatedly choose the available adjacent pair with the lowest rank and replace all non-overlapping occurrences left to right. Return tokens and decoded. Treat the whole text as one pretokenized span.

Small, well-formed JSON inputs and standard-library Python arithmetic. Keep full precision during computation. The supplied output wrapper rounds results to six decimal places.

Expected behavior

Encode UTF-8 bytes with ranked merges and reconstruct the text from token IDs.

Reproduction

01 Merge rank beats leftmost candidate
{"text":"abc","merges":[[98,99,256],[97,98,257]]}
{"tokens":[97,256],"decoded":"abc"}
02 Unmerged UTF-8 bytes
{"text":"é😀","merges":[]}
{"tokens":[195,169,240,159,152,128],"decoded":"é😀"}
03 Repeated pair merges without overlap
{"text":"aaa","merges":[[97,97,256]]}
{"tokens":[256,97],"decoded":"aaa"}

Before you begin

  • Python functions, lists and dictionaries

Need a nudge?

Reveal one investigation hint at a time. Try each clue before opening the next.

    The bug is intentional. Run the starter code to see it fail, then work toward a fix. Difficulty is provisional.
    Read-only preview
    Ready when you are
    Every fix starts with a failing test.

    Read the report, then sign in to start your investigation.