Skip to content
DeepForgeby svx
All problems
EasyNLPnlp-015

One-Hot Token Encoding

Problem

Return a one-hot list of length len(vocab) with a 1 at the index of token and 0 everywhere else.

If token is not in vocab, return an empty list.

Hint

Build a zero list and flip the single matching position.

Test cases

case 1
in: ["b",["a","b","c"]]
expected: [0,1,0]
case 2
in: ["a",["a"]]
expected: [1]
case 3
in: ["z",["a","b"]]
expected: []
case 4
in: ["c",["c","b","a"]]
expected: [1,0,0]
Starter codePython
def one_hot_token(token, vocab):
    # Your code here
    pass

4 test cases · run your code against them in the editor