Skip to content

Instantly share code, notes, and snippets.

@pzp1997
Last active August 29, 2015 14:06
Show Gist options
  • Save pzp1997/bcb2959aa4864bdcf4d5 to your computer and use it in GitHub Desktop.
Save pzp1997/bcb2959aa4864bdcf4d5 to your computer and use it in GitHub Desktop.
Command line Python program for encoding/decoding Caesar Ciphers
#!/usr/bin/env python2.7
"""Command line script to encode/decode Caesar Ciphers"""
__author__ = "Palmer Paul"
__version__ = "1.0.0"
__email__ = "[email protected]"
def caesar(pt, n):
from string import lowercase, uppercase
ct = ""
for x in range(len(pt)):
if pt[x].isalpha():
if pt[x].islower():
ct = ct + lowercase[(lowercase.find(pt[x]) + n) % 26]
else:
ct = ct + uppercase[(uppercase.find(pt[x]) + n) % 26]
else:
ct = ct + pt[x]
return ct
def main():
from sys import argv
if len(argv) != 3 or not argv[2].isdigit():
print "Usage: CaesarCipher.py ( <filename> | <string> ) <shift_val>"
raise SystemExit
try:
with open(argv[1]) as fp:
data = fp.read()
except IOError:
data = argv[1]
print caesar(data, int(argv[2]))
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment