-
-
Save nmcv/5062045 to your computer and use it in GitHub Desktop.
Calculates Index of Coincidence
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
import sys | |
import collections | |
# Bag em | |
cipher_file = open( sys.argv[ 1 ], 'rb') | |
cipher_text = cipher_file.read() | |
# remove all non alpha and whitespace and force uppercase | |
# SOTHATCIPHERTEXTLOOKSLIKETHIS | |
cipher_flat = "".join( | |
[x.upper() for x in cipher_text.split() \ | |
if x.isalpha() ] | |
) | |
# Tag em | |
N = len(cipher_flat) | |
freqs = collections.Counter( cipher_flat ) | |
alphabet = map(chr, range( ord('A'), ord('Z')+1)) | |
freqsum = 0.0 | |
# Do the math | |
for letter in alphabet: | |
freqsum += freqs[ letter ] * ( freqs[ letter ] - 1 ) | |
IC = freqsum / ( N*(N-1) ) | |
print "%.3f" % IC, "({})".format( IC ) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# Generate test suite number 1 from Sinkov pp 70 | |
# to test Index of Coincidence computation. | |
# Should produce an IC of .041 | |
al = "A" * 7 | |
al += "B" * 6 | |
al += "C" * 9 | |
al += "D" * 3 | |
al += "E" * 5 | |
al += "F" * 6 | |
al += "G" * 8 | |
al += "H" * 3 | |
al += "I" * 4 | |
al += "J" * 7 | |
al += "K" * 13 | |
al += "L" * 10 | |
al += "M" * 7 | |
al += "N" * 0 | |
al += "O" * 1 | |
al += "P" * 5 | |
al += "Q" * 3 | |
al += "R" * 6 | |
al += "S" * 8 | |
al += "T" * 5 | |
al += "U" * 4 | |
al += "V" * 8 | |
al += "W" * 4 | |
al += "X" * 8 | |
al += "Y" * 5 | |
al += "Z" * 5 | |
print al |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment