Caesar Cypher

Created on Jan. 17, 2017 by Antoine Mazières

'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']

Each letter has a position in this list. a is 1, b is 2, j is 10, etc...

Caesar cypher hide information by using a key which is a positive number to add to the position of the original letter, the result being the position of the encrypted letter.

a with key == 2 gives c

If the key brings you to the end of the alphabet, you continue to count from the begining, such as:

a with key == 28 gives c

To decypher, or decrypt, the message, you apply the same procedure moving backward on the alphabet.

You must provide the functions caesar_cypher_encrypt(s, key) and caesar_cypher_decrypt(s, key) where:

  • Write a function combining two strings so they are displayed side by side, separated by a column of pipe (|) symbols, like this:

    1
    2
    3
    4
    5
    Lorem ipsum dolor sit amet, consectetur|Lorem ipsum dolor sit amet, consectetur
    adipiscing elit. Sed non risus.        |adipiscing elit. Sed non risus.        
    Suspendisse lectus tortor, dignissim   |Suspendisse lectus tortor, dignissim   
    sit amet, adipiscing nec, utilisez sed |sit amet, adipiscing nec, utilisez sed 
    sin dolor.                             |sin dolor.     
    

    This function will also take a 3rd, optional, parameter named width representing the maximum width allowed, defaulting to 79, so the function prototype is:

    1
    2
    def sidebyside(left, right, width=79):
        ...
    

    The width should never be overflow, even with the middle | column, and the two column should be of the exact same width, so ther's two cases:

    • For an odd width: keep 1 for the middle | column, the rest is divisible by two.
    • For an even width: to avoid overflow, drop one column.

    The function should returns is a string (letter, word, sentence, etc).

  • key is a positive integer, the key of the caesar cypher.

Your implementation should only transform uppercase and lowercase ASCII letters. Special characters, numbers and letters with accents should not be transformed.

Your function shall not print but return the encoded/decoded string.

If you've written it in a caesar.py file, you can test it with:

1
2
3
4
5
6
7
8
9
>>> from caesar import caesar_cypher_encrypt, caesar_cypher_decrypt
>>> caesar_cypher_encrypt("a", 2)
'c'
>>> caesar_cypher_decrypt the new string, but should not print it.

You'll need:

Example

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30("c", 2)
'a'
>>> caesar_cypher_encrypt("Python is super disco !", 31)
'Udymts nx xzujw inxht !'
>>> caesar_cypher_decrypt("Udymts nx xzujw inxht !", 31)
'Python is super disco !'
There's no corrections yet, hit the `Submit` button to send your code to the correction bot.
Keyboard shortcuts:
  • Ctrl-Enter: Send your code to the correction bot.
  • Escape: Get back to the instructions tab.
(Ctrl-Enter)