Back to Freecodecamp

Step 70

curriculum/challenges/english/blocks/learn-string-manipulation-by-building-a-cipher/6554d25dc5ceaa354307a77e.md

latest1.6 KB
Original Source

--description--

Encryption and decryption are opposite processes and your function can do both with a couple of tweaks.

Add a third parameter called direction to your function definition. Also, comment out the last two lines of code to avoid errors in the console.

--hints--

You should turn the last two lines in your code into comments. Put a # at the beginning of each line.

js
({ test: () => assert.match(code, /#\s*encryption\s*=\s*vigenere\s*\(\s*text\s*,\s*custom_key\s*\)\s*#\s*print\s*\(\s*encryption\s*\)/) })

You should add direction as the third parameter of your function.

js
({ test: () => assert(runPython(`
    import inspect    
    sig = str(inspect.signature(vigenere))
    sig == '(message, key, direction)'
  `))
})

--seed--

--seed-contents--

py
--fcc-editable-region--
text = 'Hello Zaira'
custom_key = 'python'

def vigenere(message, key):
    key_index = 0
    alphabet = 'abcdefghijklmnopqrstuvwxyz'
    encrypted_text = ''

    for char in message.lower():
    
        # Append space to the message
        if char == ' ':
            encrypted_text += char
        else:        
            # Find the right key character to encode
            key_char = key[key_index % len(key)]
            key_index += 1

            # Define the offset and the encrypted letter
            offset = alphabet.index(key_char)
            index = alphabet.find(char)
            new_index = (index + offset) % len(alphabet)
            encrypted_text += alphabet[new_index]
    
    return encrypted_text
    
encryption = vigenere(text, custom_key)
print(encryption)
--fcc-editable-region--