To solve a project means:
Note: Joseph and I are very happy to help you out with difficulties you might have with the project tasks! You can ask questions any time!
(The project is due on 29/05/2017 before the lecture at 2pm)
Cryptography is the study of how to make messages secret or how to read secret messages. A very simple encryption technique is called the Caesar cipher, which you can read up more about here. 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, etc. (and Z will become B).
Write a python
-function cesar_crypt
that given a string and a shift (required arguments), will produce the encrypted string for that shift (return-value of the function). Note that the same function can be used to decrypt a message, by passing it a negative shift.
The rules are: you should only accept and return lowercase letters, and spaces should not be changed. You can assume that the input string of your function obeys these rules and you do not need to verify the input.
Decrypt with your function the following message, which was encrypted with a shift of 13:
pbatenghyngvbaf lbh unir fhpprrqrq va qrpelcgvat gur fgevat
Decrypt the following message and find the shift:
gwc uivioml bw nqvl bpm zqopb apqnb
If you hand in a script for this project, then please name it cesar.py
.
Hints: (1) Find below a construct that allows you to acess all letters of a string one after the other; (2) To solve the task you need to convert letters to numbers and vice versa. There are several ways to do this. One is to use the built-in functions chr
and ord
(and remember you can find out more about a function by using ?
or the Internet).
In [ ]:
# the follwing construct allows you to access all characters
# within a string one after the other:
name = "Thomas"
for letter in name:
# Within this 'for'-loop (you will learn more about it in later
# lectures) the variable 'letter' contains consecutively all
# individual letters of the string 'name'
print(letter)