-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathciphertextMessage.py
More file actions
68 lines (52 loc) · 2.46 KB
/
Copy pathciphertextMessage.py
File metadata and controls
68 lines (52 loc) · 2.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
# -*- coding: utf-8 -*-
"""
Created on Thu Nov 1 00:30:26 2018
@author: xxx
"""
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 tuple storing words to return in the end
bestTupleTillNow = ()
#tuple to compare with the best till now
prollyBest = ()
#loop looking for the best shift - using split()
for s in range(0,25):
#as long as there's sth in encrypted message
for sth in self.message_text.split():
#creating variable word, which is object type Message - for each
# element
word = Message(sth)
#if
if is_word(self.valid_words, word.apply_shift(26 - s)):
prollyBest += (word.apply_shift(26-s),)
if len(prollyBest) >= len(bestTupleTillNow):
bestTupleTillNow = prollyBest
prollyBest = ()
bestShift = 26 - s
if bestShift == 26:
bestShift = 0
else:
prollyBest = ()
return (bestShift,) + bestTupleTillNow