Workspace/Coding labs
Loading progress

Apply ordered subword merges

Intermediate65 min

Implement bpe_encode(text, merges) using character symbols and an ordered list of string pairs. Each merge makes one left-to-right nonoverlapping pass, combining matched adjacent symbols by concatenation. Require string text and pairs of two nonempty strings. Return the final symbol list. This is an educational character-based encoder, not a production byte-level tokenizer and not a merge-learning algorithm.

Your task

  1. Complete the starter function using the contract above.
  2. Use the examples and visible tests to check normal inputs, boundaries, and rejected inputs.
  3. Run tests to record your result, then compare with the explained reference solution.

Examples

EXAMPLE 1

Inputbpe_encode("banana", [("a","n"),("an","a")])

Output["b", "an", "ana"]

Earlier merges change the symbols seen by later passes.
EXAMPLE 2

Inputbpe_encode("aaaa", [("a","a")])

Output["aa", "aa"]

Matches consume input positions without overlap.
solution.pyPython 3.12