Skip to content

Instantly share code, notes, and snippets.

@s3thi
Created December 8, 2011 08:01
Show Gist options
  • Save s3thi/1446422 to your computer and use it in GitHub Desktop.
Save s3thi/1446422 to your computer and use it in GitHub Desktop.
Caesar Cipher #1
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