Created
May 25, 2018 17:16
-
-
Save douglasrizzo/d277c4c0ddeacc164628797e2659102d to your computer and use it in GitHub Desktop.
Caesar Cipher
This file contains hidden or 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
def caesar(plainText, shift): | |
"""Caesar Cipher implementation. | |
Upper and lower case letters are kept accordingly, non-alphabetic characters are ignored. | |
Implementation taken from here: https://stackoverflow.com/q/8886947/1245214 | |
:param plainText: the text to be ciphered | |
:param shift: shift value for the cipher | |
:return: the ciphered text | |
""" | |
cipherText = "" | |
for ch in plainText: | |
finalLetter = ch | |
if ch.isalpha(): | |
finalLetter = chr((ord(ch.lower()) - 97 + shift) % 26 + 97) | |
if ch.isupper(): | |
finalLetter = finalLetter.upper() | |
cipherText += finalLetter | |
return cipherText |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment