Question: import string from collections import Counter # Computed from A Tale of Two Cities. Compare Table 1 . 3 in Hoffstein, Pipher, Silverman. english _
import string
from collections import Counter
# Computed from "A Tale of Two Cities". Compare Table in Hoffstein, Pipher, Silverman.
englishfreq
a:
b:
c:
d:
e:
f:
g:
h:
i:
j:
k:
l:
m:
n:
o:
p:
q:
r:
s:
t:
u:
v:
w:
x:
y:
z:
def onlylettersX caseNone:
Returns the string obtained from X by removing everything but the letters.
If case"upper" or case"lower", then the letters are all
converted to the same case.
X joinc for c in X if c in string.asciiletters
if lenX:
return None
if case is None:
return X
elif case "lower":
return Xlower
elif case "upper":
return Xupper
def shiftcharch shiftamt:
Shifts a specific character by shiftamt.
Example:
shiftcharY returns B
if ch in string.asciilowercase:
base a
elif ch in string.asciiuppercase:
base A
# It's not clear what shifting should mean in other cases
# so if the character is not upper or lowercase, we leave it unchanged
else:
return ch
return chrordchordbaseshiftamtordbase
def shiftstringX shiftamt:
Shifts all characters in X by the same amount.
return joinshiftcharch shiftamt for ch in X
def countsubstringsXn:
Returns a Python Counter object of all ngrams in X
if not X:
return
X onlylettersX
shifts Xi: for i in rangen
grams joinchrs for chrs in zipshifts
return Countergrams
def getfreqX:
Returns the proportion that each letter occurs in X
I might change this later, but for now, it converts everything to lowercase.
The reason is to match what appears in the englishfreq dictionary.
X onlylettersX case"lower"
n lenX
ctr countsubstringsX
output
for char in string.asciilowercase:
outputchar ctrcharn
return output
def mutindcod d:
For letter frequency dictionaries d and d return the Mutual Index of Coincidence.
See Equation on page in Hoffstein, Pipher, Silverman.
s
for k in dkeys:
s dgetkdgetk
return s
Paste in a block of English text, surrounded by triple quotation marks this helps prevent errors due to apostrophes and store the resulting string using the variable s:
s Your text goes here. It can be long and include linebreaks.
How do these values compare to the percentages shown in HoffsteinPipherSilverman HPS Table on page Your answer will depend on how long your block of text was. Notice that HPS reports percentages, and the getfreq function reports probabilities or proportions.
Briefly answer this question in a markdown cell you can click the three lines on the right side of this cell and select "add markdown cell
For your same string s as above, compute the Mutual Index of Coincidence with englishfreq For suitably long English text, the value should be around As long as the value is above and below that is a good range.
Make a new string t of random text not English, just hit random keys on the keyboard, but try not to repeat the same letters to an extreme degree
Again compute the Mutual Index of Coincidence with englishfreq The new value should be closer to or
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
