Skip to content
DeepForgeby svx
All problems
EasyInformation Theoryinfo-143

Index of Coincidence

Problem

Return the index of coincidence of a text: the probability that two randomly chosen positions hold the same letter.

IC = sum n_i * (n_i - 1) / (N * (N - 1))

where n_i counts occurrences of each character. Return 0.0 when the text has fewer than two characters.

Hint

English text has an IC near 0.066, while random text sits near 0.038.

Test cases

case 1
in: ["AAAA"]
expected: 1
case 2
in: ["ABAB"]
expected: 0.3333333333333333
case 3
in: ["ABCD"]
expected: 0
case 4
in: ["AABBCC"]
expected: 0.2
case 5
in: ["HELLO"]
expected: 0.1
Starter codePython
def index_of_coincidence(text):
    # Your code here
    pass

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