Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
127 changes: 99 additions & 28 deletions exercise/ps4b.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Problem Set 4B
# Name: <your name here>
# Name: Thai Hong Curi
# Collaborators:
# Time Spent: x:xx
# Time Spent: 2:40

import string

Expand Down Expand Up @@ -58,6 +58,7 @@ def get_story_string():
### END HELPER CODE ###

WORDLIST_FILENAME = 'words.txt'
wordlist = load_words(WORDLIST_FILENAME)

class Message(object):
def __init__(self, text):
Expand All @@ -70,15 +71,24 @@ def __init__(self, text):
self.message_text (string, determined by input text)
self.valid_words (list, determined using helper function load_words)
'''
pass #delete this line and replace with your code here
#delete this line and replace with your code here
self.message_text = text
text_copy = text[:]
self.valid_words = list()
words = list(text_copy.split())

for word in words:
if is_word(wordlist, word=word):
self.valid_words.append(word)

def get_message_text(self):
'''
Used to safely access self.message_text outside of the class

Returns: self.message_text
'''
pass #delete this line and replace with your code here
#delete this line and replace with your code here
return self.message_text[:]

def get_valid_words(self):
'''
Expand All @@ -87,7 +97,8 @@ def get_valid_words(self):

Returns: a COPY of self.valid_words
'''
pass #delete this line and replace with your code here
#delete this line and replace with your code here
return self.valid_words[:]

def build_shift_dict(self, shift):
'''
Expand All @@ -103,7 +114,13 @@ def build_shift_dict(self, shift):
Returns: a dictionary mapping a letter (string) to
another letter (string).
'''
pass #delete this line and replace with your code here
#delete this line and replace with your code here
mapping = dict()
for i in range(26):
mapping[chr(ord('A')+i)] = chr(ord('A') + i + shift) if ord('A') + i + shift <= ord('Z') else chr(ord('A') + i + shift - 26)
mapping[chr(ord('a')+i)] = chr(ord('a') + i + shift) if ord('a') + i + shift <= ord('z') else chr(ord('a') + i + shift - 26)
return mapping


def apply_shift(self, shift):
'''
Expand All @@ -117,7 +134,22 @@ def apply_shift(self, shift):
Returns: the message text (string) in which every character is shifted
down the alphabet by the input shift
'''
pass #delete this line and replace with your code here
#delete this line and replace with your code here
letter = list()
mapping = self.build_shift_dict(shift)

for char in self.message_text:
letter.append(char)

for i in range(len(letter)):
if ord('A') <= ord(letter[i]) <= ord('Z') or ord('a') <= ord(letter[i]) <= ord('z'):
letter[i] = mapping[letter[i]]
else:
continue

shifted = ''.join(letter)
return shifted


class PlaintextMessage(Message):
def __init__(self, text, shift):
Expand All @@ -135,31 +167,38 @@ def __init__(self, text, shift):
self.message_text_encrypted (string, created using shift)

'''
pass #delete this line and replace with your code here
#delete this line and replace with your code here
Message.__init__(self, text)
self.shift = shift
self.encryption_dict = self.build_shift_dict(shift)
self.message_text_encrypted = self.apply_shift(shift)

def get_shift(self):
'''
Used to safely access self.shift outside of the class

Returns: self.shift
'''
pass #delete this line and replace with your code here
#delete this line and replace with your code here
return self.shift

def get_encryption_dict(self):
'''
Used to safely access a copy self.encryption_dict outside of the class

Returns: a COPY of self.encryption_dict
'''
pass #delete this line and replace with your code here

#delete this line and replace with your code here
return self.encryption_dict.copy()

def get_message_text_encrypted(self):
'''
Used to safely access self.message_text_encrypted outside of the class

Returns: self.message_text_encrypted
'''
pass #delete this line and replace with your code here
#delete this line and replace with your code here
return self.message_text_encrypted

def change_shift(self, shift):
'''
Expand All @@ -171,21 +210,25 @@ def change_shift(self, shift):

Returns: nothing
'''
pass #delete this line and replace with your code here

#delete this line and replace with your code here
self.shift = shift
self.encryption_dict = self.build_shift_dict(shift)
self.message_text_encrypted = self.apply_shift(shift)
return

class CiphertextMessage(Message):
def __init__(self, text):
'''
Initializes a CiphertextMessage object
Initializes a Ciph ertextMessage 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)
'''
pass #delete this line and replace with your code here
#delete this line and replace with your code here
Message.__init__(self, text)

def decrypt_message(self):
'''
Expand All @@ -203,22 +246,50 @@ def decrypt_message(self):
Returns: a tuple of the best shift value used to decrypt the message
and the decrypted message text using that shift value
'''
pass #delete this line and replace with your code here
#delete this line and replace with your code here
total_count = dict()

for shift in range(26):
shifted_text = self.apply_shift(shift)
shifted_words = shifted_text.split()
count = 0
for word in shifted_words:
if is_word(wordlist, word=word):
count += 1
total_count[shift] = count

max_count = 0
for i in range(len(total_count)):
if total_count[i] > max_count:
max_count = total_count[i]

for k, v in total_count.items():
if v == max_count:
break

riu_shift = k
final_shifted_text = self.apply_shift(riu_shift)
return (riu_shift, final_shifted_text)




if __name__ == '__main__':

# #Example test case (PlaintextMessage)
# plaintext = PlaintextMessage('hello', 2)
# print('Expected Output: jgnnq')
# print('Actual Output:', plaintext.get_message_text_encrypted())
#
# #Example test case (CiphertextMessage)
# ciphertext = CiphertextMessage('jgnnq')
# print('Expected Output:', (24, 'hello'))
# print('Actual Output:', ciphertext.decrypt_message())
#Example test case (PlaintextMessage)
plaintext = PlaintextMessage('hello', 2)
print('Expected Output: jgnnq')
print('Actual Output:', plaintext.get_message_text_encrypted())

#TODO: WRITE YOUR TEST CASES HERE
#Example test case (CiphertextMessage)
ciphertext = CiphertextMessage('jgnnq')
print('Expected Output:', (24, 'hello'))
print('Actual Output:', ciphertext.decrypt_message())

#TODO: WRITE YOUR TEST CASES HERE
encryp_story = CiphertextMessage(get_story_string())
print('Actual Output:', encryp_story.decrypt_message())

#TODO: best shift value and unencrypted story

pass #delete this line and replace with your code here
# delete this line and replace with your code here
Loading