-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtechnest_python_problem_set_4_3
More file actions
47 lines (37 loc) · 1.78 KB
/
technest_python_problem_set_4_3
File metadata and controls
47 lines (37 loc) · 1.78 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
class CiphertextMessage(Message):
def __init__(self, text):
'''
Initializes a CiphertextMessage object
text (string): the message's text
a CiphertextMessage object has two attributes:
self.message_text (string, determined by input text)
self.valid_words (list, determined using helper function load_words)
'''
Message.__init__(self, text)
def decrypt_message(self):
'''
Decrypt self.message_text by trying every possible shift value
and find the "best" one. We will define "best" as the shift that
creates the maximum number of real words when we use apply_shift(shift)
on the message text. If s is the original shift value used to encrypt
the message, then we would expect 26 - s to be the best shift value
for decrypting it.
Note: if multiple shifts are equally good such that they all create
the maximum number of you may choose any of those shifts (and their
corresponding decrypted messages) to return
Returns: a tuple of the best shift value used to decrypt the message
and the decrypted message text using that shift value
'''
best_shift = 0
max_words = 0
for iteration in range(26):
decrypted = self.apply_shift(iteration)
decrypted_words = decrypted.split(" ")
valid_words = 0
for word in decrypted_words:
if is_word(self.valid_words, word):
valid_words += 1
if valid_words > max_words:
max_words = valid_words
best_shift = iteration
return (best_shift, self.apply_shift(best_shift))