-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMessage.py
More file actions
89 lines (74 loc) · 3.28 KB
/
Copy pathMessage.py
File metadata and controls
89 lines (74 loc) · 3.28 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
# -*- coding: utf-8 -*-
"""
Created on Wed Oct 31 22:45:02 2018
@author: xxx
"""
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).
'''
shiftedDict = {}
for letter in string.ascii_lowercase:
try:
shiftedDict[letter] = string.ascii_lowercase[string.ascii_lowercase.index(letter)+shift]
except IndexError:
shiftedDict[letter] = string.ascii_lowercase[string.ascii_lowercase.index(letter)+shift-26]
for sth in string.ascii_uppercase:
try:
shiftedDict[sth] = string.ascii_uppercase[string.ascii_uppercase.index(sth)+shift]
except IndexError:
shiftedDict[sth] = string.ascii_uppercase[string.ascii_uppercase.index(sth)+shift-26]
return shiftedDict
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
'''
lookupDict = self.build_shift_dict(shift)
self.message_text = list(self.message_text)
for i in range (0,len(self.message_text)):
try:
self.message_text[i] = lookupDict[self.message_text[i]]
except KeyError:
pass
self.message_text = ''.join(self.message_text)
return self.message_text