-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProblem2.py
More file actions
87 lines (67 loc) · 3.46 KB
/
Copy pathProblem2.py
File metadata and controls
87 lines (67 loc) · 3.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#Problem 2 - PlaintextMessage
For this problem, the graders will use our implementation of the Message class, so don't worry
# if you did not get the previous parts correct.
# PlaintextMessage is a subclass of Message and has methods to encode a string using a specified shift value.
# Our class will always create an encoded version of the message, and will have methods for changing the encoding.
# Implement the methods in the class PlaintextMessage according to the specifications in ps6.py. The methods you
# should fill in are:
# i) __init__(self, text, shift): Use the parent class constructor to make your code more concise.
# ii) The getter method get_shift(self)
# iii) The getter method get_encrypting_dict(self): This should return a COPY of self.encrypting_dict to prevent
# someone from mutating the original dictionary.
# iv) The getter method get_message_text_encrypted(self)
# v) change_shift(self, shift): Think about what other methods you can use to make this easier. It shouldn’t take more than
# a couple lines of code.
# Paste your implementation of the entire PlaintextMessage class in the box below.
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
'''
self.shift = shift
self.message_text = text
self.valid_words = load_words(WORDLIST_FILENAME)
self.encrypting_dict = super(PlaintextMessage, self).build_shift_dict(shift)
self.message_text_encrypted = super(PlaintextMessage, 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
'''
cpy= self.encrypting_dict.copy()
return cpy
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
'''
assert shift>=0 and shift<26
self.shift = shift
self.encrypting_dict = super(PlaintextMessage, self).build_shift_dict(shift)
self.message_text_encrypted = super(PlaintextMessage, self).apply_shift(shift)