PIXELBANKv9.1.0
Menu

Build a character-level tokenizer that converts text to token IDs and back.

Given a text string, build a vocabulary mapping each unique character to an integer ID (sorted by character order, starting from 0). Then encode the text as a list of IDs and decode it back.

Input:

  • A single line of text

Output:

  • Line 1: The encoded token IDs (space-separated)
  • Line 2: The decoded text (should match input)

Example:

Input:
hello
Output:
1 0 2 2 3
hello
Reasoning:
  • First, we create a vocabulary mapping each unique character to an integer ID: h maps to 0, e maps to 1, l maps to 2, o maps to 3.
  • Then, we encode the input text "hello" using the vocabulary: h (0) is not present, so we start with h's ID which is not in the list, e's ID is 1, l's ID is 2, the next l's ID is also 2, and o's ID is 3. However, the correct mapping should be based on the sorted character order, so e maps to 0, h maps to 1, l maps to 2, o maps to 3. Thus, "hello" becomes 1 0 2 2 3.
  • The decoded text is obtained by mapping each ID back to its corresponding character: 1 maps to h, 0 maps to e, 2 maps to l, 2 maps to l, and 3 maps to o, resulting in "hello".

Constraints:

  • Characters are sorted by their Unicode code point for ID assignment
  • IDs start from 0
  • Space characters are included in the vocabulary
  • Output IDs space-separated on line 1, decoded text on line 2
solution.py

Test Results

0/0
Run code to see test results.