All problems
Triple Extraction Pattern
Problem
Extract simple subject-verb-object triples from a token list. For every position whose token is in verbs, emit [previous token, verb, next token] when both neighbors exist.
Verbs at the first or last position are skipped, and a set of verbs with no matches yields an empty list.
Hint
Iterate the interior positions from 1 to len(tokens) - 2.
Test cases
case 1
in: [["cats","eat","fish"],["eat"]]
expected: [["cats","eat","fish"]]
case 2
in: [["eat","fish"],["eat"]]
expected: []
case 3
in: [["cats","eat","fish","run"],["eat","run"]]
expected: [["cats","eat","fish"]]
case 4
in: [["a","b","c"],[]]
expected: []
case 5
in: [["sleep"],["sleep"]]
expected: []
Starter codePython
def triple_extraction_pattern(tokens, verbs):
# Your code here
pass5 test cases · run your code against them in the editor