Back to Freecodecamp

Step 23

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

latest1.9 KB
Original Source

--description--

Declare two functions named encrypt and decrypt, both with text and shift parameters.

You'll use encrypt for the encryption process, and decrypt for the decryption, labeling the two actions with a descriptive name.

Return a caesar call passing in text and shift from both your new functions, but make sure to pass in also False as the third argument for the decrypt function.

--hints--

You should have a function named encrypt with two parameters, text and shift.

js
({ test: () => assert(runPython(`_Node(_code).find_function("encrypt").has_args("text, shift")`)) })

Your encrypt function should return caesar(text, shift).

js
({ test: () => assert(runPython(`_Node(_code).find_function("encrypt").has_return("caesar(text, shift)")`)) })

You should have a function named decrypt with two parameters, text and shift.

js
({ test: () => assert(runPython(`_Node(_code).find_function("decrypt").has_args("text, shift")`)) })

Your decrypt function should return caesar(text, shift, False).

js
({ test: () => runPython(`
_n = _Node(_code).find_function("decrypt")
assert _n.has_return("caesar(text, shift, encrypt=False)") or _n.has_return("caesar(text, shift, False)")
`) })

--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

--fcc-editable-region--

--fcc-editable-region--

encrypted_text = caesar('freeCodeCamp', 3)
print(encrypted_text)