Skip to content
DeepForgeby svx
All problems
MediumNLPnlp-063

Skip-gram Context Pairs

Problem

For each center position i, pair tokens[i] with every token inside the window [i - w, i + w] except itself, scanning contexts left to right.

Return the pairs as two-element lists in center-then-context order. A window size w <= 0 yields an empty list.

Hint

Clamp the window edges to the sequence and skip the center itself.

Test cases

case 1
in: [["a","b","c"],1]
expected: [["a","b"],["b","a"],["b","c"],["c","b"]]
case 2
in: [["a","b"],2]
expected: [["a","b"],["b","a"]]
case 3
in: [["x"],1]
expected: []
case 4
in: [["a","b","c"],0]
expected: []
Starter codePython
def skipgram_pairs(tokens, w):
    # Your code here
    pass

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