Caesar Cipher
Most of us like to keep secrets. For example, I don't want you to know my credit card number. I'd rather you not be able to buy things with my money. However, communication over the Internet is like trying to shout in a crowded room. Everyone can hear what you are saying. So, to protect our valuable secrets, we rely on encryption schemes. Today, you are going to implement a simplistic encryption scheme known since Roman times: Caesar cipher.
Encryption in the Caesar schema relies on shifting letters in the alphabet. By default, this shift is 3 letters. So, a becomes d, b becomes e, etc. The only issue with this is at the end of the alphabet. The end of the alphabet wraps around to the front. So, x becomes a, y becomes b, and z becomes c.
Details
Create the function encode_caesar(plaintext)
that
encodes text using the Caesar cipher. The
parameter plaintext is a string of lowercase
characters. The function should return a string of lowercase
characters, which is equivalent to the plaintext with each
character shifted by 3.
Test Cases
print('Input: "hello"\tActual: ', encode_caesar("hello"), '\tExpected: khoor') print('Input: "goodbye"\tActual: ', encode_caesar("goodbye"), '\tExpected: jrrgebh')
-
The
ord
function gives you the numeric representation of a character. Subtract theord('a')
from this, to get the position in alphabet a character is. -
Use addition to shift the numeric representation of the
character and use the
chr
function to convert the numeric value back to a character. -
The numeric representation may be too large after the shift.
You can use the
%
operator to wrap the number around to the beginning.
Challenge
Encryption for Caesar is useful, but in order to talk to anyone
they need to be able to decrypt the Caesar encryption. Write
the function decode_caesar(ciphertext)
that
decrypts the ciphertext.