Skip to content
DeepForgeby svx
All problems
HardNLPnlp-339

Hashing Trick Bucket Counts

Problem

The hashing trick maps tokens to buckets with a deterministic polynomial hash: bucket = (sum over characters of (index + 1) * ord(ch)) mod n_buckets.

Given the tokens and the number of buckets, return the count vector.

Hint

Accumulate the position-weighted character codes before taking the modulus.

Test cases

case 1
in: [["ab"],10]
expected: [0,0,0,1,0,0,0,0,0,0]
case 2
in: [["a","a"],3]
expected: [0,2,0]
case 3
in: [[],4]
expected: [0,0,0,0]
Starter codePython
def hashing_trick_bucket_counts(tokens, n_buckets):
    # Your code here
    pass

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