Skip to content

Commit 269206f

Browse files
committed
Added Python Explanation
1 parent 16f1f1d commit 269206f

File tree

3 files changed

+123
-3
lines changed

3 files changed

+123
-3
lines changed

Python/README.md

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
# Caesar Cipher in Python
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+
4+
## Problem
5+
Implement a Caesar cipher, both encoding and decoding. The key is an integer from 1 to 25. This cipher rotates the letters of the alphabet (A to Z). The encoding replaces each letter with the 1st to 25th next letter in the alphabet (wrapping Z to A). So key 2 encrypts “HI” to “JK”, but key 20 encrypts “HI” to “BC”.
6+
7+
## Solution
8+
The caesar cipher is a substitution cipher; a cipher where each letter in the alphabet is replaced with another.
9+
This makes understanding the cipher quite easy.
10+
11+
### Caesar Cipher Explanation
12+
We have 26 letters in the alphabet.
13+
When we "shift" the letters along the order of the letters change.
14+
In our cipher, we replace any letters in the previous position with the letters in the shifted position.
15+
We can use the sentence `THE QUICK BROWN FOX JUMPS OVER THE LAZY DOG` to show this.
16+
```python
17+
Alphabet: 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 Z
18+
Text: THE QUICK BROWN FOX JUMPS OVER THE LAZY DOG
19+
Shift all letters by 1
20+
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
21+
Text: UIF RVJDL CSPXO GPY KVNQT PWFS UIF MBAZ EPH
22+
```
23+
Look how unreadable the text has become after simply replacing the letters with the next letter in the alphabet!
24+
Decoding the encoded text back into plaintext is simple: we apply the cipher in the opposite direction.
25+
So for example, if we encode "APPLE" to "KZZVO" (shifting 10 places) we can turn "KZZVO" back to "APPLE" by shifting -10 places.
26+
27+
Read more:
28+
- [Wikipedia page on the Caesar Cipher](https://en.wikipedia.org/wiki/Caesar_cipher)
29+
- [Online Caesar Cipher converter](https://cryptii.com/pipes/caesar-cipher)
30+
31+
## Python Solution Explained
32+
33+
```python
34+
# define the data we need: alphabet, text, shift amount.
35+
alphabet = "abcdefghijklmnopqrstuvwxyz"
36+
# .lower() is used to convert the text into only lower text as our alphabet only has lowercase letters.
37+
text = input("Please enter your text: ").lower()
38+
# make sure to use int() so our shift is a number not a string.
39+
shift_amount = int(input("Please enter the shift amount: "))
40+
```
41+
```python
42+
# This is very similar to the pseudocode example .
43+
new_text = ""
44+
for letter in text:
45+
if letter in alphabet:
46+
# We shift the letter only if it is in the alphabet.
47+
index = alphabet.index(letter)
48+
index += shift_amount
49+
index %= 26
50+
new_text += alphabet[index]
51+
else:
52+
# Just add any numbers/symbols/etc onto the encoded text .
53+
new_text += letter
54+
```
55+
```
56+
# Show the difference between the original and encoded text.
57+
print(f"\"{text}\" has been encoded to \"{new_text}\"")
58+
```

Python/solution.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
2+
# define the data we need: alphabet, text, shift amount.
3+
alphabet = "abcdefghijklmnopqrstuvwxyz"
4+
# .lower() is used to convert the text into only lower text as our alphabet only has lowercase letters.
5+
text = input("Please enter your text: ").lower()
6+
# make sure to use int() so our shift is a number not a string.
7+
shift_amount = int(input("Please enter the shift amount: "))
8+
9+
# This is very similar to the pseudocode example .
10+
new_text = ""
11+
for letter in text:
12+
if letter in alphabet:
13+
# We shift the letter only if it is in the alphabet.
14+
index = alphabet.index(letter)
15+
index += shift_amount
16+
index %= 26
17+
new_text += alphabet[index]
18+
else:
19+
# Just add any numbers/symbols/etc onto the encoded text .
20+
new_text += letter
21+
22+
# Show the difference between the original and encoded text.
23+
print(f"\"{text}\" has been encoded to \"{new_text}\"")

README.md

Lines changed: 42 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# CaesarCipher
1+
# Caesar Cipher
22
In this code challenge we are implementing the famous Caesar cipher used to send messages without a third-party being able to read them.
33

44
![GitHub followers](https://img.shields.io/github/followers/hrszpuk?style=social)
@@ -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
3030
Text: UIF RVJDL CSPXO GPY KVNQT PWFS UIF MBAZ EPH
3131
```
3232
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.
3335

3436
Read more:
3537
- [Wikipedia page on the Caesar Cipher](https://en.wikipedia.org/wiki/Caesar_cipher)
3638
- [Online Caesar Cipher converter](https://cryptii.com/pipes/caesar-cipher)
3739

3840
### 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.
3944

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.
4047
```python
4148
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']
4249
text = INPUT
4350
shift_amount = INPUT
4451
```
4552

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.
4662
```python
4763
new_text = EMPTY STRING
48-
FOR EACH letter IN text:
64+
FOR EACH letter IN text
4965
index = INDEX OF letter IN alphabet
50-
index = index + SHIFT_AMOUNT
66+
index = index + shift_amount
5167
index = index MOD 26
5268
new_text = new_text + alphabet[index]
5369
END
5470

5571
OUTPUT new_text
5672
```
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

Comments
 (0)