Created
December 8, 2011 08:01
-
-
Save s3thi/1446422 to your computer and use it in GitHub Desktop.
Caesar Cipher #1
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
from string import whitespace, punctuation | |
def shift(c, shift_by = 2): | |
if c in whitespace + punctuation: return c | |
upper_ord, lower_ord, c_ord = ord('A'), ord('a'), ord(c) | |
c_rel = (c_ord - lower_ord) if c_ord >= lower_ord else (c_ord - upper_ord) | |
offset = lower_ord if c_ord >= lower_ord else upper_ord | |
return chr(offset + (c_rel + shift_by) % 26) | |
msg = 'a quick brown fox jumped over the lazy dog' | |
encoded_msg = ''.join(shift(l) for l in msg) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment