-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProblem1.py
More file actions
131 lines (105 loc) · 5.16 KB
/
Copy pathProblem1.py
File metadata and controls
131 lines (105 loc) · 5.16 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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
#Problem 1 - Build the Shift Dictionary and Apply Shift
# The Message class contains methods that could be used to apply a cipher to a string, either to encrypt or
# to decrypt a message (since for Caesar codes this is the same action).
# In the next two questions, you will fill in the methods of the Message class found in ps6.py according to the
# specifications in the docstrings. The methods in the Message class already filled in are:
# __init__(self, text)
# The getter method get_message_text(self)
# The getter method get_valid_words(self), notice that this one returns a copy of self.valid_words to
# prevent someone from mutating the original list.
# In this problem, you will fill in two methods:
# i) Fill in the build_shift_dict(self, shift) method of the Message class. Be sure that your dictionary
# includes both lower and upper case letters, but that the shifted character for a lower case letter and its uppercase
# version are lower and upper case instances of the same letter. What this means is that if the original letter is "a" and
# its shifted value is "c", the letter "A" should shift to the letter "C".
# If you are unfamiliar with the ordering or characters of the English alphabet, we will be following the
# letter ordering displayed by string.ascii_lowercase and string.ascii_uppercase:
# >>> import string
# >>> print(string.ascii_lowercase)
# abcdefghijklmnopqrstuvwxyz
# >>> print(string.ascii_uppercase)
# ABCDEFGHIJKLMNOPQRSTUVWXYZ
# A reminder from the introduction page - characters such as the space character, commas, periods,
# exclamation points, etc will not be encrypted by this cipher - basically, all the characters within string.punctuation,
# plus the space (' ') and all numerical characters (0 - 9) found in string.digits.
# ii) Fill in the apply_shift(self, shift) method of the Message class. You may find it easier to use build_shift_dict(self, shift).
# Remember that spaces and punctuation should not be changed by the cipher.
class Message(object):
### DO NOT MODIFY THIS METHOD ###
def __init__(self, text):
'''
Initializes a Message object
text (string): the message's text
a Message 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)
### DO NOT MODIFY THIS METHOD ###
def get_message_text(self):
'''
Used to safely access self.message_text outside of the class
Returns: self.message_text
'''
return self.message_text
### DO NOT MODIFY THIS METHOD ###
def get_valid_words(self):
'''
Used to safely access a copy of self.valid_words outside of the class
Returns: a COPY of self.valid_words
'''
return self.valid_words[:]
def build_shift_dict(self, shift):
'''
Creates a dictionary that can be used to apply a cipher to a letter.
The dictionary maps every uppercase and lowercase letter to a
character shifted down the alphabet by the input shift. The dictionary
should have 52 keys of all the uppercase letters and all the lowercase
letters only.
shift (integer): the amount by which to shift every letter of the
alphabet. 0 <= shift < 26
Returns: a dictionary mapping a letter (string) to
another letter (string).
'''
self.shift=shift
assert self.shift>=0 and self.shift<26
lalpha=string.ascii_lowercase
ualpha=string.ascii_uppercase
dict={}
for lc in lalpha:
try:
dict[lc]=lalpha[self.shift+ lalpha.index(lc)]
except IndexError:
dict[lc]=lalpha[abs(26-(self.shift+ lalpha.index(lc)))]
for uc in ualpha:
try:
dict[uc]=ualpha[self.shift + ualpha.index(uc)]
except IndexError:
dict[uc]=ualpha[abs(26-(self.shift+ ualpha.index(uc)))]
return dict
def apply_shift(self, shift):
'''
Applies the Caesar Cipher to self.message_text with the input shift.
Creates a new string that is self.message_text shifted down the
alphabet by some number of characters determined by the input shift
shift (integer): the shift with which to encrypt the message.
0 <= shift < 26
Returns: the message text (string) in which every character is shifted
down the alphabet by the input shift
'''
self.shift=shift
assert self.shift>=0 and self.shift<26
nst=''
ndict=self.build_shift_dict(shift)
t=self.get_message_text()
for c in t:
if c in string.ascii_letters:
nst=nst + ndict[c]
elif c==' ':
nst=nst+c
elif c in string.punctuation:
nst=nst+c
elif c in string.digits:
nst=nst+c
return nst