CS108
Spring 2024

Assignment 12: Character Encoding and Ciphering

due by 9:00 p.m. EST on Tuesday 3/5/24

Preliminaries

In your work on this assignment, make sure to abide by the collaboration policies of the course.

For each problem in this problem set, we will be writing or evaluating some Python code. You are encouraged to use the Spyder IDE which will be discussed/presented in class, but you are welcome to use another IDE if you choose.

If you have questions while working on this assignment, please post them on Piazza! This is the best way to get a quick response from your classmates and the course staff.

Programming Guidelines

  • Refer to the class Coding Standards for important style guidelines. The grader will be awarding/deducting points for writing code that comforms to these standards.

  • Every program file must begin with a descriptive header comment that includes your name, username/BU email, and a brief description of the work contained in the file.

  • Every function must include a descriptive docstring that explains what the function does and identifies/defines each of the parameters to the function.

  • Your functions must have the exact names specified below, or we won’t be able to test them. Note in particular that the case of the letters matters (all of them should be lowercase), and that some of the names include an underscore character (_).

  • Make sure that your functions return the specified value, rather than printing it. None of these functions should use a print statement.

  • If a function takes more than one input, you must keep the inputs in the order that we have specified.

  • You should not use any Python features that we have not discussed in class or read about in the textbook.

  • Your functions do not need to handle bad inputs – inputs with a type or value that doesn’t correspond to the description of the inputs provided in the problem.

  • You must test your work before you submit it You can prove to yourself whether it works correctly – or not – and make corrections before submission. If you need help testing your code, please ask the course staff!

  • Do not submit work with syntax errors. Syntax errors will cause the Gradescope autograder to fail, resulting in a grade of 0.

Warnings: Individual Work and Academic Conduct!!

  • This is an individual assignment. You may discuss the problem statement/requirements, Python syntax, test cases, and error messages with your classmates. However, each student must write their own code without copying or referring to other student’s work.

  • It is strictly forbidden to use any code that you find from online websites including but not limited to as CourseHero, Chegg, or any other sites that publish homework solutions.

  • It is strictly forbidden to use any generative AI (e.g., ChatGPT or any similar tools**) to write solutions for for any assignment.

Students who submit work that is not authentically their own individual work will earn a grade of 0 on this assignment and a reprimand from the office of the Dean.

If you have questions while working on this assignment, please post them on Piazza! This is the best way to get a quick response from your classmates and the course staff.

Task 1: A Caesar Shift Cipher

100 points; individual-only

Consider the following encoded message: 'iuet kag iqdq tqdq.' What does it mean?

This is a phrase encoded by a scheme called a Caesar shift cipher. A Caesar cipher is a character substitution scheme in which each letter is replaced by another letter. Moreover, it is a scheme in which the substitutions are obtained by “shifting” some number of letters to the right or left.

For example, if we are shifting letters to the right by 3 positions: A becomes D, B becomes E, ..., X becomes A, Y becomes B, and Z becomes C. (i.e., the letters wrap around at the end of the alphabet).

Encoding/Decoding Algorithm

To decipher such a message, you’ll need to know the key to the Cipher. The key describes how many positions to shift. Let’s assume that positive shifts are to the right (i.e., a key of 1 means A becomes B) and that negative shifts are to the left (i.e., a key of -2 means that D becomes B). Note that a key of +3 is equivalent to a key of -23, since in both cases A becomes D, etc. Further, if you encode with a key of +3, you can decode with a key of -3. You only need one function to do both ciphering and deciphering.

Implementation

Write your program as a set of 3 functions, all in the file a12_caesarcipher.py.

  1. Write a function called shift_char(c, n) that will shift a single character c (a string variable that is only one character long) by n positions, and return the shifted character. For example:
    >>> shift_char('a',3)  # simple case: no wrap around needed
    'd'
    >>> shift_char('x',3)  # wrap around the end of the alphabet
    'a'
    >>> shift_char('a',-2) # wrap around the beginning of the alphabet
    'y'
    >>> shift_char(' ', 5)  # shifting a non-letter should simply same character
    ' '
    

Notes:

Reminder: Accumulator Pattern

In this function, you will practice using the accumulator design pattern to build a string by iteration. Recall that the accumulator pattern includes:

  • initialize an accumulator variable before your loop

  • update the accumulator variable inside the loop, and

  • return the accumulator variable after the loop

For each character, call your shift_char function to obtain the shifted version of that character, which you will concatenate into ciphertext. Return the ciphertext after the loop.

Here are some test cases:

>>> cipher('time', 4)
'xmqi'
>>> cipher('welcome to the machine', 19)
'pxevhfx mh max ftvabgx'
  1. Suppose you have some ciphertext (i.e., 'iuet kag iqdq tqdq', or 'prqhb') but you don’t know which decryption key (i.e., the number of characters to shift) to use.

Write a function called codebreaker(ciphertext) that will try all possible Caesar cipher shifts (there are only 26 to try) and print out each possible deciphering, one per line. One of them will stand out as a valid English phrase.

Hints:

Include 3 test cases for your program in the __main__ section, illustrating that it works correctly to decode the ciphertext examples above.


Submitting Your Work

20 points; will be assigned by code review

Log in to GradeScope to submit your work.

Be sure to name your files correctly!

You will submit two files for this assignment: a12_caesarcipher.py.

When you upload the files, the autograder will test your program.

Notes:

Warning: Beware of Global print statements

  • The autograder script cannot handle print statements in the global scope, and their inclusion causes this error:

    The autograder failed to execute correctly. Please ensure that your submission is valid. Contact your course staff for help in debugging this issue. Make sure to include a link to this page so that they can help you most effectively.

  • You can prevent this error by not having any print statements in the global scope. Instead, create an if __name__ == '__main__': section at the bottom of the file, and put any test cases/print statements in that controlled block.

  • print statements inside of functions do not cause this problem.