Answered step by step
Verified Expert Solution
Question
1 Approved Answer
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
## Import a Python helper file
Download the file LabHelper.py
Import several functions defined in that uploaded file by evaluating the following code. Copy and paste it into a new cell, and then evaluate by holding down Shift and hitting Enter.
from LabHelper import shiftstring, onlyletters, englishfreq, getfreq, mutindco
Check that shiftstring is working by evaluating the following.
X "yeah"
shiftstringX
Check that onlyletters is working by evaluating the following code. Notice that the number disappears, the same as the punctuation and spaces.
X "Hello, and welcome to Math A
onlylettersX case"lower"
Check that getfreq is working by evaluating the following code.
getfreqHello there"
The result should be a Python dictionary whose keys are the lowercase letters, and whose values are the proportions which which those letters appear in the string. For example, the letter e appears three times and there are ten total letters, so the value for e is the real number
If you evaluate englishfreq you should see a similar dictionary, with estimated proportions for "average" English text. These were calculated by computing the proportions from the book A Tale of Two Cities
The function mutindco is meant to be used with frequencies for two different pieces of text, but you can also use it by repeating the frequency for a single piece of text. This should produce a real number which is an estimate for the probability that two randomly chosen English letters are equal. The following computes the Mutual Index of Coincidence between our "average" English text and itself.
mutindcoenglishfreq, englishfreq
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started