Skip to content
DeepForgeby svx
All problems
MediumDeep Learningdl-408

Induction Head Target

Problem

Toy induction head: at the given position, look for the most recent earlier occurrence of the current token that is at least two positions back, and return the index immediately after it, which is the token an induction head would copy. Return -1 when there is no such occurrence.

The signature is induction_head_target(tokens, position).

Hint

The target is the successor of the previous matching token, which is how a repeated pattern gets completed.

Test cases

case 1
in: [[1,2,3,1,2],4]
expected: 2
case 2
in: [[5,5],1]
expected: -1
case 3
in: [[1,2,1,2],3]
expected: 2
case 4
in: [[7],0]
expected: -1
case 5
in: [[1,2,3],2]
expected: -1
Starter codePython
def induction_head_target(tokens, position):
    # Your code here
    pass

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