Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Message(object):
- ### DO NOT MODIFY THIS METHOD ###
- def __init__(self, text):
- '''
- Initializes a Message object
- text (string): the message's text
- a Message object has two attributes:
- self.message_text (string, determined by input text)
- self.valid_words (list, determined using helper function load_words
- '''
- self.message_text = text
- self.valid_words = load_words(WORDLIST_FILENAME)
- ### DO NOT MODIFY THIS METHOD ###
- def get_message_text(self):
- '''
- Used to safely access self.message_text outside of the class
- Returns: self.message_text
- '''
- return self.message_text
- ### DO NOT MODIFY THIS METHOD ###
- def get_valid_words(self):
- '''
- Used to safely access a copy of self.valid_words outside of the class
- Returns: a COPY of self.valid_words
- '''
- return self.valid_words[:]
- def build_shift_dict(self, shift):
- '''
- Creates a dictionary that can be used to apply a cipher to a letter.
- The dictionary maps every uppercase and lowercase letter to a
- character shifted down the alphabet by the input shift. The dictionary
- should have 52 keys of all the uppercase letters and all the lowercase
- letters only.
- shift (integer): the amount by which to shift every letter of the
- alphabet. 0 <= shift < 26
- Returns: a dictionary mapping a letter (string) to
- another letter (string).
- '''
- # Creating dictionary with lettter corresponding to the number it is in using the unicode associated with it.
- Low = {chr(i + 96): i for i in range(1, 27)}
- Up = {chr(i + 64): i for i in range(1, 27)}
- lower = string.ascii_lowercase
- upper = string.ascii_uppercase
- newlow = dict()
- newup = dict()
- # Iterate for each key entry now to update the key to have the right shifted letter
- # There is 26 entries in each dictionary at the start.
- for key in Low:
- # If the shifted value exceeds 26 then we need to reset the counter
- newascii = Low[key] + shift
- if (newascii) >= 27:
- newascii -= 26
- # Grab the corresponding letter with the shifted value and putting it
- # into the new dictionary
- newlow[key] = lower[newascii - 1]
- # Same thing as above but for the Uppercase section
- for key in Up:
- newascii = Up[key] + shift
- if newascii >= 27:
- newascii -= 26
- newup[key] = upper[newascii - 1]
- # Merging the two dictionaries together
- newup.update(newlow)
- # Making a copy so we dont mess with the original
- Shifted = newup.copy()
- # Returning the new dictionary with the shifted mappings.
- return Shifted
- def apply_shift(self, shift):
- '''
- Applies the Caesar Cipher to self.message_text with the input shift.
- Creates a new string that is self.message_text shifted down the
- alphabet by some number of characters determined by the input shift
- shift (integer): the shift with which to encrypt the message.
- 0 <= shift < 26
- Returns: the message text (string) in which every character is shifted
- down the alphabet by the input shift
- '''
- # Accessing / creating the cipher dictionary from the build method
- self.cipher_dict = self.build_shift_dict(self,shift)
- # Creating the encrypted message as a list initally to build a string
- encrypt = []
- for char in self.message_text:
- if char in self.cipher_dict:
- encrypt.append(self.cipher_dict[char])
- # If the char isnt in the dict. It will be added to the string as
- # It appears. (covers spaces / punctuation)
- else:
- encrypt.append(self.message_text[char])
- # Convert to a string
- encryptstring = ''.join(encrypt)
- # Output the string
- return encryptstring
- class PlaintextMessage(Message):
- def __init__(self, text, shift):
- '''
- Initializes a PlaintextMessage object
- text (string): the message's text
- shift (integer): the shift associated with this message
- A PlaintextMessage object inherits from Message and has five attributes:
- self.message_text (string, determined by input text)
- self.valid_words (list, determined using helper function load_words)
- self.shift (integer, determined by input shift)
- self.encrypting_dict (dictionary, built using shift)
- self.message_text_encrypted (string, created using shift)
- Hint: consider using the parent class constructor so less
- code is repeated
- '''
- # Initalization from Message parent class
- # Self.message_text and self.valid_words
- Message.__init__(self,text)
- # Initalization of the shift integer
- self.shift = shift
- # Building encrypting dict from the message build_shift_dict method
- self.encrypting_dict = Message.build_shift_dict(shift)
- # Encrypt the message using the message apply_shift method
- self.message_text_encrypted = Message.apply_shift(shift)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement