Back to Freecodecamp

Step 84

curriculum/challenges/english/blocks/learn-string-manipulation-by-building-a-cipher/65552a111190e11f0963949e.md

latest1.5 KB
Original Source

--description--

Delete the pass keyword, and return vigenere(message, key) from your new function.

--hints--

Your encrypt function should return vigenere(message, key). Remember to delete pass.

js
({ test: () => {
    const commentless_code = __helpers.python.removeComments(code);
    const {function_body} = __helpers.python.getDef(commentless_code, "encrypt");
    assert(function_body.match(/return\s+vigenere\s*\(\s*message\s*,\s*key\s*\)/));
    assert.notMatch(function_body, /pass/);
  }
})

--seed--

--seed-contents--

py
text = 'Hello Zaira!'
custom_key = 'python'

def vigenere(message, key, direction=1):
    key_index = 0
    alphabet = 'abcdefghijklmnopqrstuvwxyz'
    final_message = ''

    for char in message.lower():

        # Append any non-letter character to the message
        if not char.isalpha():
            final_message += char
        else:        
            # Find the right key character to encode/decode
            key_char = key[key_index % len(key)]
            key_index += 1

            # Define the offset and the encrypted/decrypted letter
            offset = alphabet.index(key_char)
            index = alphabet.find(char)
            new_index = (index + offset*direction) % len(alphabet)
            final_message += alphabet[new_index]
    
    return final_message
--fcc-editable-region--
def encrypt(message, key):
    pass

encryption = vigenere(text, custom_key)
print(encryption)
decryption = vigenere(encryption, custom_key, -1)
print(decryption)
--fcc-editable-region--