Last active
August 29, 2015 14:23
-
-
Save wmantly/4c68e51e31f1bbb5d246 to your computer and use it in GitHub Desktop.
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
| class Ceasar( str ): | |
| def __shiftLetter( self, letter, shift ): | |
| letter_code = ord( letter ) | |
| shift = int( shift ) | |
| if letter.islower(): | |
| base = ord( 'a' ) | |
| last = ord( 'z' ) | |
| else: | |
| base = ord( 'A' ) | |
| last = ord( 'Z' ) | |
| if letter_code not in range( base, last+1 ): | |
| return letter; | |
| return chr((letter_code - base + (26 + (shift)) ) % 26 + base); | |
| def encrypt( self, shift, message=None ): | |
| message = message if message else self | |
| cipher_message = '' | |
| for letter in message: | |
| cipher_message += self.__shiftLetter( letter, shift ) | |
| return __class__( cipher_message ) | |
| def decrypt( self, shift, message=None ): | |
| return self.encrypt( shift*-1, message ) | |
| message = Ceasar('hello! world zoo?') | |
| m = message.encrypt(3) | |
| print( m, m.decrypt(3) ) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment