Skip to content

Instantly share code, notes, and snippets.

@simonhayward
Last active October 7, 2015 11:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save simonhayward/3156361 to your computer and use it in GitHub Desktop.
Save simonhayward/3156361 to your computer and use it in GitHub Desktop.
Rot*
from string import ascii_uppercase as uc, ascii_lowercase as lc, maketrans
def rot_func(text, encode=True, rotate=13):
letters = uc + lc
rot = "".join((x[:rotate][::-1] + x[rotate:][::-1])[::-1] for x in (uc,lc))
src, trg = (letters, rot) if encode else (rot, letters)
trans = maketrans(src, trg)
return text.translate(trans)
text = "Text to ROT"
encoded = rot_func(text)
decoded = rot_func(encoded, encode=False)
print("""
Text:\t{text}
Encode:\t{encoded}
Decode:\t{decoded}
""".format(**locals()))
@simonhayward
Copy link
Author

For python 2.x if you wanted a different rotate than 13 - str.encode("rot13")

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment