Skip to content
DeepForgeby svx
All problems
MediumNLPnlp-042

Positional Index Search

Problem

Search a positional index, a dictionary mapping each term to a dictionary from document id as a string to sorted token positions.

Return the sorted integer document ids that contain every term in terms. Return an empty list when terms is empty or any term is missing from the index.

Hint

Intersect the document id sets and convert the surviving ids back to ints.

Test cases

case 1
in: [{"a":{"0":[0,2],"1":[1]},"b":{"0":[1],"2":[0]}},["a"]]
expected: [0,1]
case 2
in: [{"a":{"0":[0,2],"1":[1]},"b":{"0":[1],"2":[0]}},["a","b"]]
expected: [0]
case 3
in: [{"a":{"0":[0,2],"1":[1]},"b":{"0":[1],"2":[0]}},["a","z"]]
expected: []
case 4
in: [{"a":{"0":[0,2],"1":[1]},"b":{"0":[1],"2":[0]}},[]]
expected: []
Starter codePython
def positional_index_search(index, terms):
    # Your code here
    pass

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