Created
November 8, 2010 20:58
-
-
Save maxcountryman/668259 to your computer and use it in GitHub Desktop.
Extremely simple Caesar cipher
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
import argparse | |
parser = argparse.ArgumentParser(description='Shift a string n chars based on step.') | |
parser.add_argument('--shift', help='shift a string of text') | |
parser.add_argument('--deshift', help='deshift a string of text') | |
parser.add_argument('--steps', type=int) | |
args = parser.parse_args() | |
steps = args.steps | |
shift = lambda txt,sft=1: ''.join([[ch,chr((ord(ch) - ord(['A','a'][ch.islower()]) + sft)%26+ord(['A','a'][ch.islower()]))][ch.isalpha()] for ch in txt]) | |
deshift = lambda s,n: shift(s,n) if n < 0 else shift(s,-n) | |
if args.shift: | |
string = args.shift | |
print shift(string,steps) | |
if args.deshift: | |
string = args.deshift | |
print deshift(string,steps) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment