CSCI 102 - Intro to Computer Science LAB

Python Assignment

Quick Links: zyBook | Piazza | Canvas | CS @ Mines

Home | Contact | Schedule | Assignments | Syllabus |

Week 13 Python Assignment
Due by Tuesday, December 7th, 2021
        Part A: Caesar Cipher Encoder & Breaker (submit to Gradescope by 11:45pm)
        (No Part B)


Welcome to your assignment for Week 13 of CSCI 102! Each week, after Studio, you should work on that week's homework assignment (lab). This lab is to be done on your own (not pair programming), and needs to be completed and submitted to Gradescope no later than 11:45 PM on the due date. You do not need to demo this lab in class. Unlike CSCI 101, we will place all assignments for a given week on one HTML page. This is your Week 13 CSCI 102 Assignment page.

Good news: only ONE 102 lab assignment this week. And it's the last one!

Caesar Cipher Encoder & Breaker (7 points)

Introduction

To get started, open IDLE and create a New File via the File menu. We suggest you immediately save this file in the directory managing all your 102 Python Labs this semester. Please save this file with the following name: Week13A-caesar.py.

Background

In this lab, you will implement (1) a Caesar cipher encoder and (2) a brute force Caesar cipher breaker. A Caesar cipher is a simple substitution cipher in which each upper or lowercase letter is replaced by the letter x characters further down the alphabet, where x is a chosen shift in the range 0-25. For example, given the string 'Quizzes', a shift of 1 would result in 'Rvjaaft', where each letter is shifted down the alphabet once, wrapping around if necessary. The Caesar cipher is an incredibly insecure cipher, as there are only 25 possible shifts. One could easily 'brute force' the solution by trying all of the possible shifts (which is what you will do in this lab).

Assignment

For the first part of this lab, implement a Caesar cipher encoder. Your program should (1) take as inputs the message to encrypt and the shift and (2) output the final ecrypted message. A couple of notes:
  • Only letters should be shifted; do not shift symbols, numbers, spaces, or anything except letters.
  • Capitalization should be retained.
  • Your program should be able to handle both positive and negative shifts.
  • Your program should efficiently handle shifts larger than 26 and smaller than -26 (i.e., you should not go around the alphabet multiple times).
We encourage you to test your solution to the first part of this assignment, before moving on to the second part.

For the second part of this lab, implement a brute force Caesar cipher breaker. A brute force method is one that checks every possible option and chooses the best one. Your program should (1) take as input a message that has been encoded using a Caesar cipher and (2) output the original message and the shift used to encode it. A couple of notes:
  • Capitalization should be retained.
  • To simplify, there will not be any punctuation in the encoded message. That is, the input will consist of only words separated by spaces.
  • You are NOT given the shift. Instead, try every possible shift and use our dictionary.txt file to decide on the most likely original message.
  • When determining the shift, output the smallest possible positive shift that could have been used to encode the original message (e.g., 2 instead of 28).
Hints: If you find yourself copy/pasting code, make a function. Your code should execute all of the example executions below in no more than 2 seconds.

Lab I/O Format

Thoughout this semester we will use a specific Lab Input/Output Format. This format is described below:
  • When prompting for input, use the prompt string WORD>, where WORD is a single, uppercase word which describes the input. For example, your WORD in this lab might be: ( MESSAGE> and SHIFT>>) or MESSAGE_TO_DECRYPT>.
  • When providing output that will be graded, start the line with OUTPUT. Think of this as "boxing your answer" on a math worksheet; it lets us quickly find your answer. Our grading script will skip any output lines that do not start with OUTPUT.
  • You are welcome (and should!) have other output lines that do not begin with OUTPUT; while our grading script will ignore these, good programmers include print statements that are informational to the user of the program.

Example Execution #1

Input the message to encrypt and the desired shift.
MESSAGE> Hello World!
SHIFT> -9485
Your message encrypted with shift -9485:
OUTPUT Mjqqt Btwqi!

Input the message to decrypt using brute force
MESSAGE_TO_DECRYPT> Z jfcvdecp jnvri kyrk Z rd lg kf ef xffu
The original message is:
OUTPUT I solemnly swear that I am up to no good
The original message was encrypted with a shift of:
OUTPUT 17


Example Execution #2

Input the message to encrypt and the desired shift.
MESSAGE> Crazy people don't know they are crazy. I know I am crazy, therefore I am not crazy, isn't that crazy!
SHIFT> 2304958723460239458723689432509438725345293
Your message encrypted with shift 2304958723460239458723689432509438725345293:
OUTPUT Vktsr ixhiex whg'm dghp maxr tkx vktsr. B dghp B tf vktsr, maxkxyhkx B tf ghm vktsr, blg'm matm vktsr!

Input the message to decrypt using brute force.
MESSAGE_TO_DECRYPT> Hmh Izivcsri Wii Xlex Figeywi M Ampp Rsx Fi Hsmrk Mx Ekemr
The original message is:
OUTPUT Did Everyone See That Because I Will Not Be Doing It Again
The original message was encrypted with a shift of:
OUTPUT 4


Example Execution #3

Input the message to encrypt and the desired shift.
MESSAGE> Do not go gentle into that good night.
SHIFT> 0
Your message encrypted with shift 0:
OUTPUT Do not go gentle into that good night.

Input the message to decrypt using brute force.
MESSAGE_TO_DECRYPT> Rage rage against the dying of the light
The original message is:
OUTPUT Rage rage against the dying of the light
The original message was encrypted with a shift of:
OUTPUT 0


Gradescope Submission Nuances

Lab 13 will ONLY be submitted to Gradescope (you will NOT demo this lab in class).
When you submit your Python file to Gradescope, multiple different test cases are run on your code. Passing all of the tests results in a 100% on the autograded portion of the lab.

You are allowed to submit to Gradescope four times (or less) for this lab. The maximum grade of your submissions will be your grade for the lab. Note: If your code doesn’t work (e.g., a syntax error exists, or an error is thrown in execution), then you will received an AUTOMATIC ZERO. You should test your code before submitting to ensure it executes correctly.

Comments

All Python files should include a header with your name, section, assignment info, references (i.e., who did you collaborate with on this assignment?; what resource did you use?), and approximate time taken to do the assignment. Be sure to cite any allowed external references used to complete the assignment. Any code without this header will lose 1 point. Here's an example:

        #  John Henke
        #  CSCI 102 – Section B
        #  Week 13 Review Lab
        #  References: 101 Instructor Megan helped me debug.
        #  Time: 75 minutes

Submit Solutions

Follow these steps to submit your file to Gradescope.
  • In Gradescope, go to CSCI 102 > Week13A and upload Week13A-caesar.py.
To receive full credit, your code must execute in Python 3, you must submit a single file for the assignment (your Python code file), and your code must follow the proper output formatting.

Whenever you submit something to Gradescope, we strongly recommend you always double check what you submitted actually got submitted correctly (e.g., did the file upload correctly? did you submit the correct file? etc.) If your submission is incorrect, it's on you.