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
Write a function shiftdecrypt to automatically decrypt ciphertext that has been encrypted using a shift cipher. Use the following pseudocode as your template. Even if you have an alternate approach, for this assignment, you should follow this pseudocode. Or at least check with Chris first before using a different approach.
Here is a template to help you with the syntax and strategy. In this template, rather than using the above strategy, we return the shift amount for which the frequency of the letter E is highest. If you already are pretty comfortable with Python, try to see how far you can get without using this template.
import numpy as np
def naivedecryptY:
holder npzeros
for i in range:
X shiftstringY i
holderi XcountE
# npargmax finds the location of the maximum value.
# If the maximum is obtained multiple times, only the first index is returned.
shiftamt npargmaxholder
X shiftstringY shiftamt
return X shiftamt
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