HW3
py
keyboard_arrow_up
School
Georgia Institute Of Technology *
*We aren’t endorsed by this school
Course
6040
Subject
Computer Science
Date
Feb 20, 2024
Type
py
Pages
3
Uploaded by ChancellorSteelSeaUrchin34
Report
#!/usr/bin/env python
# coding: utf-8
# # Using Python for Research Homework: Week 3, Case Study 1
# # A cipher is a secret code for a language. In this case study, we will explore a cipher that is reported by contemporary Greek historians to have been used by Julius Caesar to send secret messages to generals during times of war.
# ### Exercise 1
# # A cipher is a secret code for a language. In this case study, we will explore a cipher that is reported by contemporary Greek historians to have been used by Julius Caesar to send secret messages to generals during times of war.
# # The Caesar cipher shifts each letter of a message to another letter in the alphabet located a fixed distance from the original letter. If our encryption key were `1`, we would shift `h` to the next letter `i`, `i` to the next letter `j`, and so on. If we reach the end of the alphabet, which for us is the space character, we simply loop back to `a`. To decode the message, we make a similar shift, except we move the same number of steps backwards in the alphabet.
# # Over the next five exercises, we will create our own Caesar cipher, as well as a message decoder for this cipher. In this exercise, we will define the alphabet used
in the cipher.
# # #### Instructions
# - The `string` library has been imported. Create a string called `alphabet` consisting of the space character `' '` followed by (concatenated with) the lowercase letters. Note that we're only using the lowercase letters in this exercise.
# In[3]:
import string
alphabet = ' ' + string.ascii_lowercase
# ### Exercise 2 # # In this exercise, we will define a dictionary that specifies the index of each character in `alphabet`.
# # #### Instructions # - `alphabet` has already defined in the last exercise. Create a dictionary with keys consisting of the characters in alphabet and values consisting of the numbers from 0 to 26.
# - Store this as `positions`.
# In[14]:
positions = {}
for i in range(len(alphabet)):
positions[alphabet[i]] = i
positions['n']
# ### Exercise 3
# # In this exercise, we will encode a message with a Caesar cipher.
# # #### Instructions # # - `alphabet` and `positions` have already been defined in previous exercises. Use
`positions` to create an encoded message based on message where each character in message has been shifted forward by 1 position, as defined by positions.
# - **Note that you can ensure the result remains within 0-26 using result % 27**
# - Store this as `encoded_message`.
# In[22]:
message = "hi my name is caesar"
def encode(message):
encoded_message = ''
for i in message:
for k,v in positions.items():
if v == (positions[i] + 1) %27:
encoded_message += k
return encoded_message
encode("hi my name is caesar")
# ### Exercise 4
# # In this exercise, we will define a function that encodes a message with any given
encryption key.
# # #### Instructions # - `alphabet`, `position` and `message` remain defined from previous exercises. Define a function `encoding` that takes a message as input as well as an int encryption key `key` to encode a message with the Caesar cipher by shifting each letter in message by key positions.
# - Your function should return a string consisting of these encoded letters.
# - Use `encoding` to encode message using `key = 3` and save the result as `encoded_message`.
# Print `encoded_message`.
# In[25]:
def encoding(message, key):
result = ''
for i in message:
for k,v in positions.items():
if v == (positions[i] + key) %27:
result += k
return result
encoded_message = encoding("hi my name is caesar", 3)
print(encoded_message)
# ### Exercise 5
# # In this exercise, we will decode an encoded message.
# # #### Instructions # - Use `encoding` to decode `encoded_message`.
# - Store your encoded message as `decoded_message`.
# - Print `decoded_message`. Does this recover your original message?
# In[26]:
# In[28]:
decoded_message = encoding(encoded_message, -3)
print(decoded_message)
# In[ ]:
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
- Access to all documents
- Unlimited textbook solutions
- 24/7 expert homework help
Related Documents
Recommended textbooks for you
Text book image
Programming Logic & Design Comprehensive
Computer Science
ISBN:9781337669405
Author:FARRELL
Publisher:Cengage
Text book image
EBK JAVA PROGRAMMING
Computer Science
ISBN:9781337671385
Author:FARRELL
Publisher:CENGAGE LEARNING - CONSIGNMENT
Text book image
EBK JAVA PROGRAMMING
Computer Science
ISBN:9781305480537
Author:FARRELL
Publisher:CENGAGE LEARNING - CONSIGNMENT
Text book image
C++ Programming: From Problem Analysis to Program...
Computer Science
ISBN:9781337102087
Author:D. S. Malik
Publisher:Cengage Learning
Text book image
Principles of Information Systems (MindTap Course...
Computer Science
ISBN:9781285867168
Author:Ralph Stair, George Reynolds
Publisher:Cengage Learning
Text book image
C++ for Engineers and Scientists
Computer Science
ISBN:9781133187844
Author:Bronson, Gary J.
Publisher:Course Technology Ptr
Recommended textbooks for you
- Text book imageProgramming Logic & Design ComprehensiveComputer ScienceISBN:9781337669405Author:FARRELLPublisher:CengageText book imageEBK JAVA PROGRAMMINGComputer ScienceISBN:9781337671385Author:FARRELLPublisher:CENGAGE LEARNING - CONSIGNMENTText book imageEBK JAVA PROGRAMMINGComputer ScienceISBN:9781305480537Author:FARRELLPublisher:CENGAGE LEARNING - CONSIGNMENT
- Text book imageC++ Programming: From Problem Analysis to Program...Computer ScienceISBN:9781337102087Author:D. S. MalikPublisher:Cengage LearningText book imagePrinciples of Information Systems (MindTap Course...Computer ScienceISBN:9781285867168Author:Ralph Stair, George ReynoldsPublisher:Cengage LearningText book imageC++ for Engineers and ScientistsComputer ScienceISBN:9781133187844Author:Bronson, Gary J.Publisher:Course Technology Ptr
Text book image
Programming Logic & Design Comprehensive
Computer Science
ISBN:9781337669405
Author:FARRELL
Publisher:Cengage
Text book image
EBK JAVA PROGRAMMING
Computer Science
ISBN:9781337671385
Author:FARRELL
Publisher:CENGAGE LEARNING - CONSIGNMENT
Text book image
EBK JAVA PROGRAMMING
Computer Science
ISBN:9781305480537
Author:FARRELL
Publisher:CENGAGE LEARNING - CONSIGNMENT
Text book image
C++ Programming: From Problem Analysis to Program...
Computer Science
ISBN:9781337102087
Author:D. S. Malik
Publisher:Cengage Learning
Text book image
Principles of Information Systems (MindTap Course...
Computer Science
ISBN:9781285867168
Author:Ralph Stair, George Reynolds
Publisher:Cengage Learning
Text book image
C++ for Engineers and Scientists
Computer Science
ISBN:9781133187844
Author:Bronson, Gary J.
Publisher:Course Technology Ptr