📘
One-Hot Sequence Encoder
Given a vocabulary and a sequence of words, encode each word as a one-hot vector.
Input format:
- Line 1: Comma-separated vocabulary (sorted)
- Line 2: Space-separated words to encode
Each word becomes a vector of length |vocab| with 1 at the word's index and 0 elsewhere. If a word is not in the vocabulary, use all zeros.
Output: One vector per line as a list.
Example:
Input:
cat,dog,fish dog cat fish
Output:
[0, 1, 0] [1, 0, 0] [0, 0, 1]
Reasoning:
Step 1: Build vocabulary index cat=0, dog=1, fish=2
Step 2: Encode each word "dog" → index 1 → [0, 1, 0] "cat" → index 0 → [1, 0, 0] "fish" → index 2 → [0, 0, 1]
Constraints:
- Vocabulary is comma-separated and sorted
- Each output vector has length = vocabulary size
- Unknown words get all-zero vectors
- Output each vector as a Python list
Editor
Python 3.13.1
Test Results
0/0Run code to see test results.