Explore Library
Code QuizIntermediate

Padding Adds Too Many Tokens

Spot why this padding routine overshoots the target length.

Codepython
def pad_sequence(seq, maxlen, pad=0):
    if len(seq) > maxlen:
        return seq[:maxlen]
    return seq + [pad] * maxlen

print(pad_sequence([1, 2], 4))

What is the bug in this pad/truncate function?