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).
- '''
- d = {}
- for i in range(26):
- d[string.ascii_lowercase[i]] = string.ascii_lowercase[(i + shift) % 26]
- d[string.ascii_uppercase[i]] = string.ascii_uppercase[(i + shift) % 26]
- return d
- 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
- '''
- m = self.get_message_text()
- di = self.build_shift_dict(shift)
- res = ''
- for ltr in m:
- if ltr in di.keys():
- res += di[ltr]
- else:
- res += ltr
- return res
- 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
- '''
- Message.__init__(self, text)
- self.shift = shift
- self.encrypting_dict = self.build_shift_dict(shift)
- self.message_text_encrypted = self.apply_shift(shift)
- def get_shift(self):
- '''
- Used to safely access self.shift outside of the class
- Returns: self.shift
- '''
- return self.shift
- def get_encrypting_dict(self):
- '''
- Used to safely access a copy self.encrypting_dict outside of the class
- Returns: a COPY of self.encrypting_dict
- '''
- return self.encrypting_dict.copy()
- def get_message_text_encrypted(self):
- '''
- Used to safely access self.message_text_encrypted outside of the class
- Returns: self.message_text_encrypted
- '''
- return self.message_text_encrypted
- def change_shift(self, shift):
- '''
- Changes self.shift of the PlaintextMessage and updates other
- attributes determined by shift (ie. self.encrypting_dict and
- message_text_encrypted).
- shift (integer): the new shift that should be associated with this message.
- 0 <= shift < 26
- Returns: nothing
- '''
- self.shift = shift
- self.encrypting_dict = self.build_shift_dict(shift)
- self.message_text_encrypted = self.apply_shift(shift)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement