Created
January 28, 2011 02:58
-
-
Save mhayes/799746 to your computer and use it in GitHub Desktop.
Python translation capabilities
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
""" | |
Decode a super-secret message | |
""" | |
import sys | |
from string import maketrans | |
# setup translation | |
in_wrds = "abcdefghijklmnopqrstuvwxyz" | |
out_wrds = "cdefghijklmnopqrstuvwxyzab" | |
trans_tbl = maketrans(in_wrds, out_wrds) | |
# now translate | |
if len(sys.argv) > 0: | |
print sys.argv[1].translate(trans_tbl) | |
else: | |
puzzle = open("puzzle.txt", "r") | |
for line in puzzle: | |
print line.translate(trans_tbl) |
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
g fmnc wms bgblr rpylqjyrc gr zw fylb. rfyrq ufyr amknsrcpq ypc dmp. bmgle gr gl zw fylb gq glcddgagclr ylb rfyr'q ufw rfgq rcvr gq qm jmle. sqgle qrpgle.kyicrpylq() gq pcamkkclbcb. lmu ynnjw ml rfc spj. |
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
def rotate_letter(letter, pos): | |
""" | |
Rotate a letter by n positions | |
>>> rotate_letter('a', 2) | |
'b' | |
""" | |
if letter.isalpha(): | |
letter = letter.lower() | |
chr(ord(letter) + 2) | |
adjusted_pos = ord(letter) - ord('a') + pos | |
mod_pos = adjusted_pos % 26 | |
return chr(mod_pos + 97) | |
else: | |
return letter | |
temp = "" | |
f = open('puzzle.txt', 'r') | |
for l in f: | |
for c in l: | |
temp += rotate_letter(c, 2) | |
print temp |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment