Skip to content

Instantly share code, notes, and snippets.

@snahor
Created Oct 16, 2017
Embed
What would you like to do?
Caesar Cipher
from functools import wraps
a_ord = ord('a')
class NonAlphaException(Exception):
pass
def check(func):
@wraps(func)
def wrapper(alpha, offset):
if not alpha.isalpha():
raise NonAlphaException
return func(alpha, offset)
return wrapper
@check
def encrypt(alpha, offset):
return chr(a_ord + ((ord(alpha) - a_ord + offset) % 26))
@check
def decrypt(alpha, offset):
return chr(a_ord + ((ord(alpha) - a_ord - offset) % 26))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment