Assignment #02: using the standard library

Today's exercises have to be done with help of the python standard library alone! No external module can be used.

Exercise #02-01: cryptography for dummies Roman Emperors

A very simple encryption technique is called the Caesar cipher. The basic idea is that each letter is replaced by a letter that is a certain number of letters away, so for example if the shift was 2, then A would become C, B would become D, and Z will become B.

A. Write a function that given a string and a shift, will produce the encrypted string for that shift. The rules are:

  • you should accept lowercase and uppercase letters, and return letters of the same case
  • spaces or other punctuation characters should not be changed.

There are several ways to reach this result. Just pick the one which makes more sense to you. Then, decrypt the following message, which was encrypted with a shift of 13:

Pbatenghyngvbaf, lbh unir fhpprrqrq va qrpelcgvat gur fgevat.

B. Now write a decoding script which, when run in the linux or windows command line, prints the decoded phrase after asking the user to enter a phrase and a shift with which it was encrypted. Test your script on the sentence above.

C. Now try to decrypt this sentence, for which the shift is unknown:

Gwc uivioml bw nqvl bpm zqopb apqnb.

One way to solve this problem involves human decision, but another way can be fully automated (and implies more work). Pick the one you want!

Tip: make use of python's input() function

Back to the table of contents