Question
Here is the code for the classes Cipher and CipherCracker: class Cipher: def __init__(self, codestring): alphabet = ABCDEFGHIJKLMNOPQRSTUVWXYZ self.codestring = codestring.upper() code = {} inverse
Here is the code for the classes Cipher and CipherCracker:
class Cipher: def __init__(self, codestring): alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" self.codestring = codestring.upper() code = {} inverse = {} w = codestring cs = w[:] for a in 'ZYXWVUTSRQPONMLKJIHGFEDCBA': if a not in w: cs = cs + a
self.code = {b:a for (a,b) in zip(cs, alphabet)} self.inverse = {a:b for (a,b) in zip(cs, alphabet)}
def encode(self, plaintext): plaintext = plaintext.upper() plaintext1 = "".join([self.code[i] if i in self.code else i for i in plaintext]) return plaintext1
def decode(self, ciphertext): ciphertext = ciphertext.upper() ciphertext1 = "".join([self.inverse[i] if i in self.inverse else i for i in ciphertext]) return ciphertext1
from words import wordlist from cipher import Cipher
class CipherCracker: def __init__(self, ciphertext): self.preamble = ciphertext[:30] self.ciphertext = ciphertext[30:]
def decode(self, i, j): codeword = self.preamble[i:j] decoded = Cipher(codeword) return decoded.decode(self.ciphertext)
def quality(self, i, j): """ Decodes the message using self.preamble[i:j] as the codeword and returns a number that gives an indication of how many of the words in the decoded string are real words. It does this by checking if the words are in the `wordlist` variable imported from `words.py`.
There are several ways this could be done. The most important thing is that for a real message, the correct values of i and j should give a higher output than other values. """ pass
def mostlikelycodeword(self): """ Return the codeword that has the highest quality score among all substrings of the preamble. """ pass
def mostlikelydecode(self): """ Return the decoded message that you get by decoding with the most likely codeword. """ pass
Codeword Substitution One approach to defining a codestring for a substitution cipher is to specify a codeword instead. The idea is that a codeword (with no repeating letters is given) and that is used as the start of the codestring. The rest of the codestring is filled in with the remaining letters of the alphabet in reverse order. For example, if the codeword is ZEBRA, then the codestring would be: ZEBRAYXWVUTSQPONMLKJIHGFDC If the codeword is DOG , then the codestring would be: DOGZYXWVUTSRQPNMLKJIHFECBA You will want to be able to generate such a codestring from a codeword. Here is the code to extend the codeword into a codestring. w= ' ZEBRA' for a in 'ZYXWVUTSRQPONMLKJIHGFEDCBA' if a not in w cs = cs + a print (cs) ZEBRAYXWVUTSQPONMLKJIHGFDC Codeword Substitution One approach to defining a codestring for a substitution cipher is to specify a codeword instead. The idea is that a codeword (with no repeating letters is given) and that is used as the start of the codestring. The rest of the codestring is filled in with the remaining letters of the alphabet in reverse order. For example, if the codeword is ZEBRA, then the codestring would be: ZEBRAYXWVUTSQPONMLKJIHGFDC If the codeword is DOG , then the codestring would be: DOGZYXWVUTSRQPNMLKJIHFECBA You will want to be able to generate such a codestring from a codeword. Here is the code to extend the codeword into a codestring. w= ' ZEBRA' for a in 'ZYXWVUTSRQPONMLKJIHGFEDCBA' if a not in w cs = cs + a print (cs) ZEBRAYXWVUTSQPONMLKJIHGFDC
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