All problems
Windowed Type-Token Ratio
Problem
Split tokens into consecutive windows of size k, where the final window may be shorter, and return the type-token ratio of each window in order.
A window's ratio is its number of distinct tokens divided by its length. Return an empty list when k <= 0 or tokens is empty.
Hint
Step through the token list with range(0, n, k) and slice each window.
Test cases
case 1
in: [["a","b","a","b","c","d"],2]
expected: [1,1,1]
case 2
in: [["a","b","a","b","c","d"],4]
expected: [0.5,1]
case 3
in: [["a","b","a","b","c","d"],3]
expected: [0.6666666666666666,1]
case 4
in: [[],3]
expected: []
case 5
in: [["a","b"],0]
expected: []
Starter codePython
def windowed_ttr(tokens, k):
# Your code here
pass5 test cases · run your code against them in the editor