-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProblem3.py
More file actions
77 lines (62 loc) · 3.77 KB
/
Copy pathProblem3.py
File metadata and controls
77 lines (62 loc) · 3.77 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
#Problem 3 - CiphertextMessage
For this problem, the graders will use our implementation of the Message and PlaintextMessage classes,
# so don't worry if you did not get the previous parts correct.
# Given an encrypted message, if you know the shift used to encode the message, decoding it is trivial.
# If message is the encrypted message, and s is the shift used to encrypt the message, then apply_shift(message, 26-s)
# gives you the original plaintext message. Do you see why?
# The problem, of course, is that you don’t know the shift. But our encryption method only has 26 distinct possible
# values for the shift! We know English is the main language of these emails, so if we can write a program that tries
# each shift and maximizes the number of English words in the decoded message, we can decrypt their cipher! A simple
# indication of whether or not the correct shift has been found is if most of the words obtained after a shift are valid words.
# Note that this only means that most of the words obtained are actual words. It is possible to have a message that can be
# decoded by two separate shifts into different sets of words. While there are various strategies for deciding between
# ambiguous decryptions, for this problem we are only looking for a simple solution.
# Fill in the methods in the class CiphertextMessage acording to the specifications in ps6.py. T
# he methods you should fill in are:
# i) __init__(self, text): Use the parent class constructor to make your code more concise.
# ii) decrypt_message(self): You may find the helper function is_word(wordlist, word) and the
# string method split() useful. Note that is_word will ignore punctuation and other special characters when
# considering whether a word is valid.
# Hints:
# Using string.split
# You may find the function string.split useful for dividing the text up into words.
# >>> 'Hello world!'.split('o')
# ['Hell', ' w', 'rld!']
# >>> '6.00.1x is pretty fun'.split(' ')
# ['6.00.1x', 'is', 'pretty', 'fun']
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)
'''
self.message_text = text
self.valid_words = load_words(WORDLIST_FILENAME)
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
'''
word_counter = 0
max_count = 0
for i in range(26):
for j in list(super(CiphertextMessage, self).apply_shift(i).split(' ')):
if is_word(self.valid_words, j):
word_counter += 1
if word_counter > max_count:
max_count = word_counter
shift_value = i
decrypted_msg = super(CiphertextMessage, self).apply_shift(i)
return (shift_value, decrypted_msg)