Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

~ using the below code please help with the rest ~ import collections def letter_to_num(c): return ord(c)-65 def num_to_letter(n): return chr(n) def text_to_nums(m): l =

~ using the below code please help with the rest ~ import collections

def letter_to_num(c): return ord(c)-65

def num_to_letter(n): return chr(n)

def text_to_nums(m): l = [] for c in m: l.append(letter_to_num(c)) return l

def nums_to_text(l): txt = "" for num in l: txt += num_to_letter(65+num) return txt

def append_new(l,n): if n not in l: l.append(n)

def remove_dup(l): new_l =[] for item in l: append_new(new_l,item) return new_l

def extend(l): for i in range(26): append_new(l,i) return l

def cycle1(l): l.insert(0, l.pop()) return l

def cycle(l, n): for i in range(n): cycle1(l) return l

def mapping(keyword,letter): temp = extend(remove_dup(text_to_nums(keyword))) num = letter_to_num(letter) return cycle(temp,num)

def find_item(l,n): return l[n]

def find_index(l,n): return l.index(n)

def enc_list(key, l): lst = [] for i in l: lst.append(find_item(key,i)) return lst def dec_list(key,l): lst = [] for i in l: lst.append(find_index(key,i)) return lst

def encrypt(key, letter, message): temp1 = mapping(key, letter) temp2 = text_to_nums(message) encyptedlist = enc_list(temp1,temp2) return nums_to_text(encryptedList)

def add_enc(text, key): cipherText = "" for ch in text: if ch.isalpha(): stayInAlphabet = ord(ch) + key if stayInAlphabet > ord('z'): stayInAlphabet -= 26 finalLetter = chr(stayInAlphabet) cipherText += finalLetter return cipherText

def add_dec(text, key): cipherText1 = "" for ch in text: if ch.isalpha(): stayInAlphabet1 = ord(ch) - key if stayInAlphabet1 > ord('z'): stayInAlphabet1 += 26 finalLetter1 = chr(stayInAlphabet1) cipherText1 += finalLetter1 return cipherText1

def add_count(L, n): L[n] += 1 return L

~ using the above please help with the rest ~

image text in transcribed

image text in transcribed

image text in transcribed

image text in transcribed

image text in transcribed

Exercise 4 Write a function to count the occurrence of each letter in the given text. Use text_to_nums f get_frequency(text): >>> get_frequency("KNOWLEDGEISPOWERFRANCEISBACON') get_frequency('NQRZOHGJHLVSRZHUIUDQFHLVEDFRQ) 1 4 Crack the additive cipher Now we are going to crack the additive cipher. The trick is relatively straightforward. We can de- termine the key with some simple arithmetic from the encryption of letter which is the most frequent letter in the ciphertext. Exercise 5 Write a function to determine the index of the largest number in a list. def get_largest_index(1): >>>get_largest_index ([0, 0, 0, 2, 1, 2, 1, 4, 1, 1, 0, 2, 0, 1, 1, Exercise 6 Write a function to determine the most probable key given the ciphertext from an ad ditive cipher. >>get_frequency ( 'NQRZOHGJHLVSRZHUIUDQFHLVEDFRQ') >get_largest_index( [0, 0, 0, 2, 1, 2, 1, 4, 1, 1, 0, 2, 0, 1, 1, 0, 3 7-4 def crack_additive(text): >>crack_additive( "NORZOHGJHLVSRZHUIUDQFHL VEDFRQ) Exercise 4 Write a function to count the occurrence of each letter in the given text. Use text_to_nums f get_frequency(text): >>> get_frequency("KNOWLEDGEISPOWERFRANCEISBACON') get_frequency('NQRZOHGJHLVSRZHUIUDQFHLVEDFRQ) 1 4 Crack the additive cipher Now we are going to crack the additive cipher. The trick is relatively straightforward. We can de- termine the key with some simple arithmetic from the encryption of letter which is the most frequent letter in the ciphertext. Exercise 5 Write a function to determine the index of the largest number in a list. def get_largest_index(1): >>>get_largest_index ([0, 0, 0, 2, 1, 2, 1, 4, 1, 1, 0, 2, 0, 1, 1, Exercise 6 Write a function to determine the most probable key given the ciphertext from an ad ditive cipher. >>get_frequency ( 'NQRZOHGJHLVSRZHUIUDQFHLVEDFRQ') >get_largest_index( [0, 0, 0, 2, 1, 2, 1, 4, 1, 1, 0, 2, 0, 1, 1, 0, 3 7-4 def crack_additive(text): >>crack_additive( "NORZOHGJHLVSRZHUIUDQFHL VEDFRQ)

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

Recommended Textbook for

More Books

Students also viewed these Databases questions