Back to Freecodecamp

Step 77

curriculum/challenges/english/blocks/learn-string-manipulation-by-building-a-cipher/6555d6e12c95701172b55709.md

latest1.7 KB
Original Source

--description--

Functions can be called with default arguments. A default argument indicates the value that the function should take if the argument is not passed. For example, the function below accepts three arguments but you can call it passing two arguments. The third one will assume the specified value by default:

py
def foo(a, b, c=value):
    <code>

Modify the vigenere function so that its direction parameter has a default value of 1.

--hints--

The direction parameter of your vigenere function should have a default value of 1.

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

--seed--

--seed-contents--

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

--fcc-editable-region--
def vigenere(message, key, direction):
--fcc-editable-region--
    key_index = 0
    alphabet = 'abcdefghijklmnopqrstuvwxyz'
    final_message = ''

    for char in message.lower():
    
        # Append space to the message
        if char == ' ':
            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
    
encryption = vigenere(text, custom_key, 1)
print(encryption)
decryption = vigenere(encryption, custom_key, -1)
print(decryption)