2_caesar module¶
Caesar Cipher¶
A Caesar cipher is a simple substitution cipher in which each letter
of the plain text is substituted with a letter found by moving
n
places down the alphabet.
For example, assume the input plain text is the following:
abcd xyz
If the shift value, n
, is 4
,
then the encrypted text would be the following:
efgh bcd
You are to write a function that accepts two arguments, a plain-text message and a number of letters to shift in the cipher. The function will return an encrypted string with all letters transformed and all punctuation and whitespace remaining unchanged.
Note
You can assume the plain text is all lowercase ASCII except for whitespace and punctuation.
-
2_caesar.
cipher_caesar
(text: str, n: int) → str[source]¶ Encrypt or decrypt the letters in a string preserving all non letters characters (such as punctuation) according to the Caesar method: shift the letters of
n
position.- Parameters
text (str) – string to be decrypted or encrypted.
n (int) – number of position to shift the letters.
- Returns
decrypted/encrypted string.
- Return type
int
-
2_caesar.
cipher_caesar_v2
(text: str, n: int) → str[source]¶ Second version of the using list comprehension. More concise. Slower on single letters, faster on longer strings than V1.
Encrypt or decrypt the letters in a string preserving all non letters characters (such as punctuation) according to the Caesar method: shift the letters of
n
position.- Parameters
text (str) – string to be decrypted or encrypted.
n (int) – number of position to shift the letters.
- Returns
decrypted/encrypted string.
- Return type
int
-
2_caesar.
cipher_caesar_v3
(text: str, n: int) → str[source]¶ Second version of the using
str
’stranslate
function. Even more concise, slower on single characters but definitely faster than V2 on long strings.Encrypt or decrypt the letters in a string preserving all non letters characters (such as punctuation) according to the Caesar method: shift the letters of
n
position.- Parameters
text (str) – string to be decrypted or encrypted.
n (int) – number of position to shift the letters.
- Returns
decrypted/encrypted string.
- Return type
int