Back to Freecodecamp

Step 25

curriculum/challenges/english/blocks/workshop-caesar-cipher/681a44f4c3235f7d8f428545.md

latest2.9 KB
Original Source

--description--

Now you're going to test the decrypt function. Replace the value assigned to encrypted_text with the following string, which represents a message to decrypt: Pbhentr vf sbhaq va hayvxryl cynprf.

Then, declare a variable named decrypted_text and assign it a call to decrypt with encrypted_text as it first argument and a shift of 13 as the second argument.

Finally, print the decrypted_text on the terminal. With that, the Caesar cipher is complete.

--hints--

You should assign Pbhentr vf sbhaq va hayvxryl cynprf. to encrypted_text.

js
({ test: () => assert(runPython(`_Node(_code).find_variable("encrypted_text").is_equivalent("encrypted_text = 'Pbhentr vf sbhaq va hayvxryl cynprf.'")`)) })

You should have a variable named decrypted_text.

js
({ test: () => assert(runPython(`_Node(_code).has_variable("decrypted_text")`)) })

You should assign decrypt(encrypted_text, 13) to decrypted_text.

js
({ test: () => assert(runPython(`_Node(_code).find_variable("decrypted_text").is_equivalent("decrypted_text = decrypt(encrypted_text, 13)")`)) })

You should print decrypted_text.

js
({ test: () => assert(runPython(`_Node(_code).has_call("print(decrypted_text)")`)) })

--seed--

--seed-contents--

py
def caesar(text, shift, encrypt=True):

    if not isinstance(shift, int):
        return 'Shift must be an integer value.'

    if shift < 1 or shift > 25:
        return 'Shift must be an integer between 1 and 25.'

    alphabet = 'abcdefghijklmnopqrstuvwxyz'

    if not encrypt:
        shift = - shift
    
    shifted_alphabet = alphabet[shift:] + alphabet[:shift]
    translation_table = str.maketrans(alphabet + alphabet.upper(), shifted_alphabet + shifted_alphabet.upper())
    encrypted_text = text.translate(translation_table)
    return encrypted_text

def encrypt(text, shift):
    return caesar(text, shift)
    
def decrypt(text, shift):
    return caesar(text, shift, encrypt=False)

--fcc-editable-region--
encrypted_text = encrypt('freeCodeCamp', 3)
print(encrypted_text)
--fcc-editable-region--

--solutions--

py
def caesar(text, shift, encrypt=True):

    if not isinstance(shift, int):
        return 'Shift must be an integer value.'

    if shift < 1 or shift > 25:
        return 'Shift must be an integer between 1 and 25.'

    alphabet = 'abcdefghijklmnopqrstuvwxyz'

    if not encrypt:
        shift = - shift
    
    shifted_alphabet = alphabet[shift:] + alphabet[:shift]
    translation_table = str.maketrans(alphabet + alphabet.upper(), shifted_alphabet + shifted_alphabet.upper())
    encrypted_text = text.translate(translation_table)
    return encrypted_text

def encrypt(text, shift):
    return caesar(text, shift)
    
def decrypt(text, shift):
    return caesar(text, shift, encrypt=False)

encrypted_text = 'Pbhentr vf sbhaq va hayvxryl cynprf.'
decrypted_text = decrypt(encrypted_text, 13)
print(decrypted_text)