Last active
February 22, 2022 19:26
-
-
Save AndyNovo/fab6a3f90d52ebc70a5f3042e917d63b to your computer and use it in GitHub Desktop.
This file contains hidden or 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 urllib2, ssl | |
response = urllib2.urlopen("https://vip.udel.edu/crypto/mobydick.txt", context=ssl._create_unverified_context()) | |
mobytext = response.read() | |
onlyletters = filter(lambda x: x.isalpha(), mobytext) | |
loweronly = onlyletters.lower() | |
frequency = {} | |
for ascii in range(ord('a'), ord('a')+26): | |
frequency[chr(ascii)] = float(loweronly.count(chr(ascii)))/len(loweronly) | |
sum_freqs_squared = 0.0 | |
for ltr in frequency: | |
sum_freqs_squared += frequency[ltr]*frequency[ltr] | |
print "Should be near .065 if plain: " + str(sum_freqs_squared) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment