Skip to content
DeepForgeby svx
All problems
EasyNLPnlp-016

Type-Token Ratio

Problem

Return the type-token ratio: the number of distinct tokens divided by the total number of tokens.

Return 0.0 when the token list is empty.

Hint

Types are unique tokens; divide by the token count.

Test cases

case 1
in: [["a","b","c","a"]]
expected: 0.75
case 2
in: [["a","a","a"]]
expected: 0.3333333333333333
case 3
in: [[]]
expected: 0
case 4
in: [["the","cat","sat","on","the","mat"]]
expected: 0.8333333333333334
Starter codePython
def type_token_ratio(tokens):
    # Your code here
    pass

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