Advertisement
Guest User

Untitled

a guest
Apr 23rd, 2019
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.79 KB | None | 0 0
  1. class Message(object):
  2.     ### DO NOT MODIFY THIS METHOD ###
  3.     def __init__(self, text):
  4.         '''
  5.        Initializes a Message object
  6.                
  7.        text (string): the message's text
  8.  
  9.        a Message object has two attributes:
  10.            self.message_text (string, determined by input text)
  11.            self.valid_words (list, determined using helper function load_words
  12.        '''
  13.         self.message_text = text
  14.         self.valid_words = load_words(WORDLIST_FILENAME)
  15.  
  16.     ### DO NOT MODIFY THIS METHOD ###
  17.     def get_message_text(self):
  18.         '''
  19.        Used to safely access self.message_text outside of the class
  20.        
  21.        Returns: self.message_text
  22.        '''
  23.         return self.message_text
  24.  
  25.     ### DO NOT MODIFY THIS METHOD ###
  26.     def get_valid_words(self):
  27.         '''
  28.        Used to safely access a copy of self.valid_words outside of the class
  29.        
  30.        Returns: a COPY of self.valid_words
  31.        '''
  32.         return self.valid_words[:]
  33.        
  34.     def build_shift_dict(self, shift):
  35.         '''
  36.        Creates a dictionary that can be used to apply a cipher to a letter.
  37.        The dictionary maps every uppercase and lowercase letter to a
  38.        character shifted down the alphabet by the input shift. The dictionary
  39.        should have 52 keys of all the uppercase letters and all the lowercase
  40.        letters only.        
  41.        
  42.        shift (integer): the amount by which to shift every letter of the
  43.        alphabet. 0 <= shift < 26
  44.  
  45.        Returns: a dictionary mapping a letter (string) to
  46.                 another letter (string).
  47.        '''
  48.         d = {}
  49.         for i in range(26):
  50.             d[string.ascii_lowercase[i]] = string.ascii_lowercase[(i + shift) % 26]
  51.             d[string.ascii_uppercase[i]] = string.ascii_uppercase[(i + shift) % 26]
  52.         return d
  53.  
  54.     def apply_shift(self, shift):
  55.         '''
  56.        Applies the Caesar Cipher to self.message_text with the input shift.
  57.        Creates a new string that is self.message_text shifted down the
  58.        alphabet by some number of characters determined by the input shift        
  59.        
  60.        shift (integer): the shift with which to encrypt the message.
  61.        0 <= shift < 26
  62.  
  63.        Returns: the message text (string) in which every character is shifted
  64.             down the alphabet by the input shift
  65.        '''
  66.         m = self.get_message_text()
  67.         di = self.build_shift_dict(shift)
  68.         res = ''
  69.         for ltr in m:
  70.             if ltr in di.keys():
  71.                 res += di[ltr]
  72.             else:
  73.                 res += ltr
  74.         return res
  75.  
  76. class PlaintextMessage(Message):
  77.     def __init__(self, text, shift):
  78.         '''
  79.        Initializes a PlaintextMessage object        
  80.        
  81.        text (string): the message's text
  82.        shift (integer): the shift associated with this message
  83.  
  84.        A PlaintextMessage object inherits from Message and has five attributes:
  85.            self.message_text (string, determined by input text)
  86.            self.valid_words (list, determined using helper function load_words)
  87.            self.shift (integer, determined by input shift)
  88.            self.encrypting_dict (dictionary, built using shift)
  89.            self.message_text_encrypted (string, created using shift)
  90.  
  91.        Hint: consider using the parent class constructor so less
  92.        code is repeated
  93.        '''
  94.         Message.__init__(self, text)
  95.         self.shift = shift
  96.         self.encrypting_dict = self.build_shift_dict(shift)
  97.         self.message_text_encrypted = self.apply_shift(shift)
  98.  
  99.     def get_shift(self):
  100.         '''
  101.        Used to safely access self.shift outside of the class
  102.        
  103.        Returns: self.shift
  104.        '''
  105.         return self.shift
  106.  
  107.     def get_encrypting_dict(self):
  108.         '''
  109.        Used to safely access a copy self.encrypting_dict outside of the class
  110.        
  111.        Returns: a COPY of self.encrypting_dict
  112.        '''
  113.         return self.encrypting_dict.copy()
  114.  
  115.     def get_message_text_encrypted(self):
  116.         '''
  117.        Used to safely access self.message_text_encrypted outside of the class
  118.        
  119.        Returns: self.message_text_encrypted
  120.        '''
  121.         return self.message_text_encrypted
  122.    
  123.     def change_shift(self, shift):
  124.         '''
  125.        Changes self.shift of the PlaintextMessage and updates other
  126.        attributes determined by shift (ie. self.encrypting_dict and
  127.        message_text_encrypted).
  128.        
  129.        shift (integer): the new shift that should be associated with this message.
  130.        0 <= shift < 26
  131.  
  132.        Returns: nothing
  133.        '''
  134.         self.shift = shift
  135.         self.encrypting_dict = self.build_shift_dict(shift)
  136.         self.message_text_encrypted = self.apply_shift(shift)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement