Explore Library
Code QuizIntermediate

N-Gram Extraction Off-by-One

Spot the loop-range bug that produces incomplete trailing n-grams.

Codepython
def ngrams(tokens, n):
    result = []
    for i in range(len(tokens)):
        result.append(tuple(tokens[i:i+n]))
    return result

print(ngrams(['a', 'b', 'c'], 2))

What is the bug in this n-gram extraction function?