encrypt ======= Encodes a given string with the caesar cipher and returns the encoded message Parameters: ----------- * `input_string`: the plain-text that needs to be encoded * `key`: the number of letters to shift the message by Optional: * `alph
(input_string: str, key: int, alphabet: str | None = None)
| 4 | |
| 5 | |
| 6 | def encrypt(input_string: str, key: int, alphabet: str | None = None) -> str: |
| 7 | """ |
| 8 | encrypt |
| 9 | ======= |
| 10 | |
| 11 | Encodes a given string with the caesar cipher and returns the encoded |
| 12 | message |
| 13 | |
| 14 | Parameters: |
| 15 | ----------- |
| 16 | |
| 17 | * `input_string`: the plain-text that needs to be encoded |
| 18 | * `key`: the number of letters to shift the message by |
| 19 | |
| 20 | Optional: |
| 21 | |
| 22 | * `alphabet` (``None``): the alphabet used to encode the cipher, if not |
| 23 | specified, the standard english alphabet with upper and lowercase |
| 24 | letters is used |
| 25 | |
| 26 | Returns: |
| 27 | |
| 28 | * A string containing the encoded cipher-text |
| 29 | |
| 30 | More on the caesar cipher |
| 31 | ========================= |
| 32 | |
| 33 | The caesar cipher is named after Julius Caesar who used it when sending |
| 34 | secret military messages to his troops. This is a simple substitution cipher |
| 35 | where every character in the plain-text is shifted by a certain number known |
| 36 | as the "key" or "shift". |
| 37 | |
| 38 | Example: |
| 39 | Say we have the following message: |
| 40 | ``Hello, captain`` |
| 41 | |
| 42 | And our alphabet is made up of lower and uppercase letters: |
| 43 | ``abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ`` |
| 44 | |
| 45 | And our shift is ``2`` |
| 46 | |
| 47 | We can then encode the message, one letter at a time. ``H`` would become ``J``, |
| 48 | since ``J`` is two letters away, and so on. If the shift is ever too large, or |
| 49 | our letter is at the end of the alphabet, we just start at the beginning |
| 50 | (``Z`` would shift to ``a`` then ``b`` and so on). |
| 51 | |
| 52 | Our final message would be ``Jgnnq, ecrvckp`` |
| 53 | |
| 54 | Further reading |
| 55 | =============== |
| 56 | |
| 57 | * https://en.m.wikipedia.org/wiki/Caesar_cipher |
| 58 | |
| 59 | Doctests |
| 60 | ======== |
| 61 | |
| 62 | >>> encrypt('The quick brown fox jumps over the lazy dog', 8) |
| 63 | 'bpm yCqks jzwEv nwF rCuxA wDmz Bpm tiHG lwo' |
no outgoing calls
no test coverage detected