MIT 6.100L Introduction to CS and Programming Using Python, Fall 2022 · Problem set 4, Part B, 2.3) PlaintextMessage — __init__, get_pad and get_ciphertext · Dr. Ana Bell · CC BY-NC-SA 4.0 · MIT publishes no solution for this set; the worked solution is ours
Write the constructor __init__(self, input_text, pad=None) and the getters get_pad(self) and get_ciphertext(self) of MIT's PlaintextMessage class, a child class of the Message class you built in unit 17.
For this problem set, we will use a parent class called Message, which has two child classes: PlaintextMessage and EncryptedMessage.
Message contains methods that both plaintext and encrypted messages will need to use. For example, a method to get the text of the message. The child classes will inherit these shared methods from their parent.PlaintextMessage contains methods that are specific to a plaintext message, such as a method for generating a one time pad or encrypting a message.EncryptedMessage contains methods that are specific to a ciphertext, such as a method to decrypt a message given a one time pad.Your finished Message class is in the starter as given code. Fill in these methods of the PlaintextMessage class according to the specifications in the docstrings:
__init__(self, input_text, pad=None)
super()) in this method to make your code more concise. (super().__init__(input_text) runs the parent's __init__ on this object. It is the short form of Message.__init__(self, input_text), the call-the-parent-by-name form Person used for Animal.__init__.)pad=None indicates an optional argument that can be omitted and the specified default value of None is passed in instead. For example, PlaintextMessage('test') and PlaintextMessage('test', [0,15,3,9]) are both valid constructors but the former should generate a random pad and the latter should use the specified pad.pad as an attribute and not pad directly to protect it from being mutated.PlaintextMessage object has three attributes: the message text; the pad (a list of integers, determined by pad, or generated randomly using self.generate_pad() if pad is None); and the ciphertext (a string, input_text encrypted using the pad).get_pad(self): this should return a copy of self.pad to prevent someone from mutating the original list.get_ciphertext(self): used to access the ciphertext produced by applying the pad to the message text.generate_pad and change_pad are the next two challenges, so leave their stubs as they are. Still write the branch of __init__ that calls self.generate_pad() when pad is None: one test checks it with a subclass of PlaintextMessage whose generate_pad returns a fixed pad, so it works before you have written the real one.
MIT has also implemented a __repr__ method so that when you print out your PlaintextMessage objects it returns a nice human readable result. Please do not change it.
p = PlaintextMessage('hello', [3, 0, 10, 11, 4]) print(p.get_text()) # hello print(p.get_pad()) # [3, 0, 10, 11, 4] print(p.get_ciphertext()) # kevws print(PlaintextMessage('test', [0, 15, 3, 9]).get_ciphertext()) # ttv} print(p) # PlaintextMessage('hello', [3, 0, 10, 11, 4])