Created
March 16, 2014 01:24
-
-
Save Tethik/9577038 to your computer and use it in GitHub Desktop.
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
import sys | |
''' | |
Returns a list of sorted tuples of the letter distribution. | |
''' | |
def count(text): | |
letter_counts = dict() | |
#~ for char in alphabet.alphabetstr: | |
#~ letter_counts[char] = 0 | |
for char in text: | |
if not char in letter_counts.keys(): | |
letter_counts[char] = 0 | |
letter_counts[char] += 1 | |
return letter_counts | |
def index_of_coincidence_chunked(text, length): | |
chunks, chunk_size = len(text)/length, length | |
rows = [ text[i:i+chunk_size] for i in range(0, chunks, chunk_size)] | |
sum = 0.0 | |
for i in xrange(length): | |
# Grab column i of each row and join. Then compute index of that. | |
# This is equal to checking for a shift cipher if that column | |
# is using that | |
column = "".join([row[i] for row in rows]) | |
if(len(column) < 2): | |
continue | |
sum += index_of_coincidence(column) | |
# Average | |
return sum / length | |
def index_of_coincidence(text): | |
distrib = count(text) | |
divisor = len(text) * (len(text) - 1) | |
sum = 0.0 | |
for c in distrib.keys(): | |
fi = distrib[c] | |
sum += fi * (fi - 1) | |
return sum / divisor | |
if __name__ == "__main__": | |
text = "" | |
line = sys.stdin.readline() | |
while line != "": | |
text += line.strip() | |
line = sys.stdin.readline() | |
_min, _max = 1, 30 | |
if(len(sys.argv) > 2): | |
_min, _max = int(sys.argv[1]), int(sys.argv[2]) | |
#~ fig = figure() | |
#~ ax = fig.add_subplot(111, autoscale_on=False) | |
x, y = list(),list() | |
for i in range(_min, _max): | |
val = index_of_coincidence_chunked(text, i) | |
x.append(i) | |
y.append(val) | |
try: | |
import numpy as np | |
import matplotlib.pyplot as plt | |
import matplotlib.lines as lines | |
from pylab import figure, show | |
plt.plot(x,y) | |
plt.show() | |
except: | |
print zip(x,y) | |
#~ ax.add_line(lines.Line2D(x,y, color='m')) | |
#~ a=lines.Line2D([5,6],[7,8],color='m') | |
#~ ax.add_line(a) | |
#~ print x | |
#~ print y | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment