All problems
Bag of Words
Problem
Build a bag-of-words vector for a list of tokens given a fixed vocabulary.
Return a list where index i is the count of vocab[i] in tokens.
Test cases
case 1
in: [["a","b","a"],["a","b","c"]]
expected: [2,1,0]
case 2
in: [["the","cat","sat"],["the","cat","dog"]]
expected: [1,1,0]
case 3
in: [[],["a","b"]]
expected: [0,0]
case 4
in: [["x","y","z"],["a","b","c"]]
expected: [0,0,0]
Starter codePython
def bag_of_words(tokens, vocab):
# Your code here
pass4 test cases · run your code against them in the editor