Last active
April 12, 2020 06:48
-
-
Save RaminMammadzada/451848c6f7b66af2be35e00878e333b1 to your computer and use it in GitHub Desktop.
Ceaser Cipher in Python
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
Implement Caesar’s cipher: implement a function encrypt that | |
given a plaintext string and a key 𝑘 (how many letters to shift), | |
returns a ciphertext where each character is shifted 𝑘 places. | |
(You can assume all characters are lowercase letters, with no punctuation or spaces.) | |
""" | |
alphabet = 'abcdefghijklmnopqrstuvwxyz' | |
def encrypt(plaintext="hithere", k=4): | |
ciphertext = "" | |
for letter in plaintext: | |
letterLocation = 0 | |
for alpLetter in alphabet: | |
if (letter == alpLetter): | |
break | |
letterLocation += 1 | |
cipherLetter = alphabet[letterLocation - k] | |
ciphertext = ciphertext + cipherLetter | |
return ciphertext | |
print( encrypt() ) | |
#encrypt() | |
# the fastest may to get the location of the letter in the string | |
# alpLetterLocation = alphabet.index(letter) + k | |
# encrypted_data += alphabet[alpLetterLocation] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment