Question: * Write a function ` prop _ div ` using the following template. > Input: a list of integers ` X ` and a modulus
Write a function propdiv using the following template.
Input: a list of integers X and a modulus m
Output: the proportion of integers in X which are divisible by m
For example, propdiv should be equal to because out of of these integers are divisible by
def propdivX m:
n lenX
t # Eventually t will be the number of integers divisible by m in X
for x in X:
if x:
t t
return # Return the proportion, not t
Write a function keylengthkasiski to predict the key length using the following template. Recall that we introduced the kasiskidiffs function above. The idea is to find for which key length value, from keystartinclusive to keyendexclusive is the highest proportion of gaps between repeated trigrams divisible by that key length. The parameters topvalue and topkey represent the current highest proportion and the current best key length, respectively.
Feel free to rewrite this if you would like to use a more elegantPythonic approach to find the best key length, as long as it is still using the Kasiski methodIf I were doing this in Math I think I would make a pandas Series propser with values equal to the proportions and keys equal to the key lengths, and then I would find the best key length by using propser.idxmax
def keylengthkasiskiY keystart keyend:
diffs kasiskidiffsY
topvalue
topkey
for k in rangekeystart, keyend:
prop propdivdiffs k
if :
topvalue prop
topkey k
return # Return the key length which yields the highest value
Test your function by evaluating the following. The output should be
Y zpgdl rjlaj kpylx zpyyg lrjgd lrzhz qyjzq repvm swrzy rigzh
zvreg kwivs saolt nliuw oldie aqewf iiykh bjowr hdogc qhkwa
jyagg emisr zqoqh oavlk bjofr ylvps rtgiu avmsw lzgms evwpc
dmjsv jqbrn klpcf iowhv kxjbj pmfkr qthtk ozrgq ihbmq sbivd
ardym qmpbu nivxm tzwqv gefjh ucbor vwpcd xuwft qmoow jipds
fluqm oeavl jgqea lrkti wvext vkrrg xani'
keylengthkasiskiY
Write a function coincidencemean using the following template. Note Base Python does not have a builtin mean or average function, but it does have a built in sum function.
Inputs: a string Y and an integer m
Output: a real number
def coincidencemeanY m:
coinclist
for i in rangem:
s # Get the characters in Y at integer positions modulo
c # Compute the Index of Coincidence for s using the indco function
coinclist.appendc
return # Return the mean value of coinclist
Write a function keylengthcoincidence to predict the key length using the following template.
Again, feel free to rewrite this if you would like to use a more elegant approach, as long as it is still using the coincidencemean function from above.
def keylengthcoincidenceY keystart keyend:
topvalue
topkey
for k in rangekeystart, keyend:
mean coincidencemean
if :
topvalue
topkey
return # Return the key length which yields the highest value
Test your function by evaluating the following. The output should again be
Y zpgdl rjlaj kpylx zpyyg lrjgd lrzhz qyjzq repvm swrzy rigzh
zvreg kwivs saolt nliuw oldie aqewf iiykh bjowr hdogc qhkwa
jyagg emisr zqoqh oavlk bjofr ylvps rtgiu avmsw lzgms evwpc
dmjsv jqbrn klpcf iowhv kxjbj pmfkr qthtk ozrgq ihbmq sbivd
ardym qmpbu nivxm tzwqv gefjh ucbor vwpcd xuwft qmoow jipds
fluqm oeavl jgqea lrkti wvext vkrrg xani'
keylengthcoincidenceY
Paste in and execute the code for your shiftdecrypt function from Lab
Write a function findshifts using the following template.
Inputs: a Vigenre ciphertext string Y and a keylength k
Output: a list of shift amounts shifts
def findshiftsY k:
shifts
for i in rangek:
s # Get the characters in Y at integer positions modulo
X m shiftdecrypts # We only care about the shift amount m
shifts.appendm
return shifts
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
