|
1 | | -# CaesarCipher |
| 1 | +# Caesar Cipher |
2 | 2 | In this code challenge we are implementing the famous Caesar cipher used to send messages without a third-party being able to read them. |
3 | 3 |
|
4 | 4 |  |
@@ -30,27 +30,66 @@ New alphabet: Z A B C D E G F H I J K L M N O P Q R S T U V W X Y |
30 | 30 | Text: UIF RVJDL CSPXO GPY KVNQT PWFS UIF MBAZ EPH |
31 | 31 | ``` |
32 | 32 | Look how unreadable the text has become after simply replacing the letters with the next letter in the alphabet! |
| 33 | +Decoding the encoded text back into plaintext is simple: we apply the cipher in the opposite direction. |
| 34 | +So for example, if we encode "APPLE" to "KZZVO" (shifting 10 places) we can turn "KZZVO" back to "APPLE" by shifting -10 places. |
33 | 35 |
|
34 | 36 | Read more: |
35 | 37 | - [Wikipedia page on the Caesar Cipher](https://en.wikipedia.org/wiki/Caesar_cipher) |
36 | 38 | - [Online Caesar Cipher converter](https://cryptii.com/pipes/caesar-cipher) |
37 | 39 |
|
38 | 40 | ### Pseudo code solution |
| 41 | +To get started, the code here is written in pseudocode which isn't a programming language, but a language that is simple |
| 42 | +and easy to read, so anyone can read it and understand the logic behind the code. |
| 43 | +After understanding the logic implementing your own version of the code in your preferred programming language becomes easier. |
39 | 44 |
|
| 45 | +First off, we declare a few variables: alphabet, which is a list containing all the letters in the alphabet. |
| 46 | +We also get the text the user wants encoding, and the shift amount which is the amount of letters we will shift each letter. |
40 | 47 | ```python |
41 | 48 | alphabet = ['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'] |
42 | 49 | text = INPUT |
43 | 50 | shift_amount = INPUT |
44 | 51 | ``` |
45 | 52 |
|
| 53 | +Next it's time to actually encode the text. |
| 54 | +We define `new_text` which will store the encoded text. |
| 55 | +For each letter in text we do the following: |
| 56 | +1. Get the index of that letter in the alphabet (e.g. a is 1, b is 2, c is 3, ...etc) |
| 57 | +2. We add the `shift_amount` to the index (if our letter was "a" so an index of 1 it would now be 1 + `shift_amount`) |
| 58 | +3. Then we use `mod` (short for [modulo](https://www.mathsisfun.com/definitions/modulo-operation.html)) to ensure the `index` is within 26 (which is the range of our alphabet). |
| 59 | +4. And finally, we add the new letter to `new_text` but getting the letter at that index of the alphabet (e.g. alphabet[1] = a) |
| 60 | + |
| 61 | +After we have looped over every letter in the `text` we output `new_text` which is our encoded text. |
46 | 62 | ```python |
47 | 63 | new_text = EMPTY STRING |
48 | | -FOR EACH letter IN text: |
| 64 | +FOR EACH letter IN text |
49 | 65 | index = INDEX OF letter IN alphabet |
50 | | - index = index + SHIFT_AMOUNT |
| 66 | + index = index + shift_amount |
51 | 67 | index = index MOD 26 |
52 | 68 | new_text = new_text + alphabet[index] |
53 | 69 | END |
54 | 70 |
|
55 | 71 | OUTPUT new_text |
56 | 72 | ``` |
| 73 | + |
| 74 | +It's important to read the problem carefully, in this code challenge we must encode **and decode** a message from the user. |
| 75 | +However, our code already facilitates this functionality. |
| 76 | +As mentioned in "Caesar Cipher Explanation", to decode an encoded message we can apply the opposite shift amount. |
| 77 | +In this sense, our code can both encode and decode ciphered text. |
| 78 | + |
| 79 | +This solution will not accept anything that is not in the alphabet. So numbers, or symbols will break this code. |
| 80 | +In order for the solution to accept numbers and symbols we must check if the letter is in the alphabet and if not |
| 81 | +then add it to `new_text` without any processing done to it. Example below. |
| 82 | +```python |
| 83 | +new_text = EMPTY STRING |
| 84 | +FOR EACH letter IN text |
| 85 | + IF letter IN alphabet THEN |
| 86 | + index = INDEX OF letter IN alphabet |
| 87 | + index = index + shift_amount |
| 88 | + index = index MOD 26 |
| 89 | + new_text = new_text + alphabet[index] |
| 90 | + ELSE THEN |
| 91 | + new_text = new_text + letter |
| 92 | +END |
| 93 | + |
| 94 | +OUTPUT new_text |
| 95 | +``` |
0 commit comments