Advertisement
Guest User

Cipher 2

a guest
Oct 21st, 2019
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.41 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. text (string): the message's text
  7. a Message object has two attributes:
  8. self.message_text (string, determined by input text)
  9. self.valid_words (list, determined using helper function load_words
  10. '''
  11. self.message_text = text
  12. self.valid_words = load_words(WORDLIST_FILENAME)
  13.  
  14. ### DO NOT MODIFY THIS METHOD ###
  15. def get_message_text(self):
  16. '''
  17. Used to safely access self.message_text outside of the class
  18.  
  19. Returns: self.message_text
  20. '''
  21. return self.message_text
  22.  
  23. ### DO NOT MODIFY THIS METHOD ###
  24. def get_valid_words(self):
  25. '''
  26. Used to safely access a copy of self.valid_words outside of the class
  27.  
  28. Returns: a COPY of self.valid_words
  29. '''
  30. return self.valid_words[:]
  31.  
  32. def build_shift_dict(self, shift):
  33. '''
  34. Creates a dictionary that can be used to apply a cipher to a letter.
  35. The dictionary maps every uppercase and lowercase letter to a
  36. character shifted down the alphabet by the input shift. The dictionary
  37. should have 52 keys of all the uppercase letters and all the lowercase
  38. letters only.
  39.  
  40. shift (integer): the amount by which to shift every letter of the
  41. alphabet. 0 <= shift < 26
  42.  
  43. Returns: a dictionary mapping a letter (string) to
  44. another letter (string).
  45. '''
  46. # Creating dictionary with lettter corresponding to the number it is in using the unicode associated with it.
  47. Low = {chr(i + 96): i for i in range(1, 27)}
  48. Up = {chr(i + 64): i for i in range(1, 27)}
  49. lower = string.ascii_lowercase
  50. upper = string.ascii_uppercase
  51. newlow = dict()
  52. newup = dict()
  53. # Iterate for each key entry now to update the key to have the right shifted letter
  54. # There is 26 entries in each dictionary at the start.
  55. for key in Low:
  56. # If the shifted value exceeds 26 then we need to reset the counter
  57. newascii = Low[key] + shift
  58. if (newascii) >= 27:
  59. newascii -= 26
  60. # Grab the corresponding letter with the shifted value and putting it
  61. # into the new dictionary
  62. newlow[key] = lower[newascii - 1]
  63. # Same thing as above but for the Uppercase section
  64. for key in Up:
  65. newascii = Up[key] + shift
  66. if newascii >= 27:
  67. newascii -= 26
  68. newup[key] = upper[newascii - 1]
  69. # Merging the two dictionaries together
  70. newup.update(newlow)
  71. # Making a copy so we dont mess with the original
  72. Shifted = newup.copy()
  73. # Returning the new dictionary with the shifted mappings.
  74. return Shifted
  75.  
  76. def apply_shift(self, shift):
  77. '''
  78. Applies the Caesar Cipher to self.message_text with the input shift.
  79. Creates a new string that is self.message_text shifted down the
  80. alphabet by some number of characters determined by the input shift
  81.  
  82. shift (integer): the shift with which to encrypt the message.
  83. 0 <= shift < 26
  84.  
  85. Returns: the message text (string) in which every character is shifted
  86. down the alphabet by the input shift
  87. '''
  88. # Accessing / creating the cipher dictionary from the build method
  89. self.cipher_dict = self.build_shift_dict(self,shift)
  90. # Creating the encrypted message as a list initally to build a string
  91. encrypt = []
  92. for char in self.message_text:
  93. if char in self.cipher_dict:
  94. encrypt.append(self.cipher_dict[char])
  95. # If the char isnt in the dict. It will be added to the string as
  96. # It appears. (covers spaces / punctuation)
  97. else:
  98. encrypt.append(self.message_text[char])
  99. # Convert to a string
  100. encryptstring = ''.join(encrypt)
  101. # Output the string
  102. return encryptstring
  103.  
  104. class PlaintextMessage(Message):
  105. def __init__(self, text, shift):
  106. '''
  107. Initializes a PlaintextMessage object
  108. text (string): the message's text
  109. shift (integer): the shift associated with this message
  110.  
  111. A PlaintextMessage object inherits from Message and has five attributes:
  112. self.message_text (string, determined by input text)
  113. self.valid_words (list, determined using helper function load_words)
  114. self.shift (integer, determined by input shift)
  115. self.encrypting_dict (dictionary, built using shift)
  116. self.message_text_encrypted (string, created using shift)
  117.  
  118. Hint: consider using the parent class constructor so less
  119. code is repeated
  120. '''
  121. # Initalization from Message parent class
  122. # Self.message_text and self.valid_words
  123. Message.__init__(self,text)
  124. # Initalization of the shift integer
  125. self.shift = shift
  126.  
  127. # Building encrypting dict from the message build_shift_dict method
  128. self.encrypting_dict = Message.build_shift_dict(shift)
  129.  
  130. # Encrypt the message using the message apply_shift method
  131. self.message_text_encrypted = Message.apply_shift(shift)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement