Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

* Write a function ` prop _ div ` using the following template. > Input: a list of integers ` X ` and a modulus

* Write a function `prop_div` 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, `prop_div([3,5,8,9,10],3)` should be equal to `0.4`, because 2 out of 5 of these integers are divisible by 3.
```
def prop_div(X, m):
n = len(X)
t =0 # Eventually t will be the number of integers divisible by m in X
for x in X:
if x%???==???:
t = t+1
return ??? # Return the proportion, not t.
```
* Write a function `key_length_kasiski` to predict the key length using the following template. (Recall that we introduced the `kasiski_diffs` function above.) The idea is to find for which key length value, from `key_start`(inclusive) to `key_end`(exclusive), is the highest proportion of gaps between repeated trigrams divisible by that key length. The parameters `top_value` and `top_key` 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 elegant/Pythonic approach to find the best key length, as long as it is still using the *Kasiski method*.(If I were doing this in Math 10, I think I would make a pandas Series `prop_ser` with values equal to the proportions and keys equal to the key lengths, and then I would find the best key length by using `prop_ser.idxmax()`.)
```
def key_length_kasiski(Y, key_start=7, key_end=12):
diffs = kasiski_diffs(Y)
top_value =0
top_key =0
for k in range(key_start, key_end):
prop = prop_div(diffs, k)
if ???>???:
top_value = prop
top_key = k
return ??? # Return the key length which yields the highest value
```
* Test your function by evaluating the following. The output should be `7`.
```
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'''
key_length_kasiski(Y)
```
* Write a function `coincidence_mean` using the following template. (Note. Base Python does not have a built-in 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 coincidence_mean(Y, m):
coinc_list =[]
for i in range(m):
s = # Get the characters in `Y` at integer positions ??? modulo ???
c = # Compute the Index of Coincidence for `s` using the `ind_co` function
coinc_list.append(c)
return ??? # Return the mean value of `coinc_list`
```
* Write a function `key_length_coincidence` 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 `coincidence_mean` function from above.
```
def key_length_coincidence(Y, key_start=7, key_end=12):
top_value =0
top_key =0
for k in range(key_start, key_end):
mean = coincidence_mean(???,???)
if ???>???:
top_value =???
top_key =???
return ??? # Return the key length which yields the highest value
```
* Test your function by evaluating the following. The output should again be `7`.
```
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'''
key_length_coincidence(Y)
```
* Paste in and execute the code for your `shift_decrypt` function from Lab 0.
* Write a function `find_shifts` using the following template.
> Inputs: a Vigenre ciphertext string `Y` and a key-length `k`
> Output: a list of shift amounts `shifts`
```
def find_shifts(Y, k):
shifts =[]
for i in range(k):
s = # Get the characters in `Y` at integer positions ??? modulo ???
X, m = shift_decrypt(s) # We only care about the shift amount `m`
shifts.append(m)
return shifts
```

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions