Short Rot13 Function - Python

Short rot13 function - Python

maketrans()/translate() solutions…

Python 2.x

import string
rot13 = string.maketrans(
"ABCDEFGHIJKLMabcdefghijklmNOPQRSTUVWXYZnopqrstuvwxyz",
"NOPQRSTUVWXYZnopqrstuvwxyzABCDEFGHIJKLMabcdefghijklm")
string.translate("Hello World!", rot13)
# 'Uryyb Jbeyq!'

Python 3.x

rot13 = str.maketrans(
'ABCDEFGHIJKLMabcdefghijklmNOPQRSTUVWXYZnopqrstuvwxyz',
'NOPQRSTUVWXYZnopqrstuvwxyzABCDEFGHIJKLMabcdefghijklm')
'Hello World!'.translate(rot13)
# 'Uryyb Jbeyq!'

Decode python rot13 string

To decode a rot13 encoded string, say s, simply take rot13 of the string once again, i.e. compute rot13(s).

If you are not familiar how to compute rot13 in python, try googling a bit and you'll surely find one. I googled and found a solution that works pretty well : https://stackoverflow.com/a/3269756/3293087 [Note that it works only on python2 and not python3 since string.maketrans was removed from python3.]

I'll write the code here for completeness :

# Python 2 solution
import string
rot13Table = string.maketrans(
"ABCDEFGHIJKLMabcdefghijklmNOPQRSTUVWXYZnopqrstuvwxyz",
"NOPQRSTUVWXYZnopqrstuvwxyzABCDEFGHIJKLMabcdefghijklm")
rot13 = lambda s : string.translate(s, rot13Table)

print rot13("Hello World!")
# Outputs "Uryyb Jbeyq!"

Note : I'm not going to write the decoded string corresponding to what the OP originally posted, since its quite offensive and homophobic. Anyone interested can do it themselves.

Update : I also found a solution that works for python 2 & 3 both from this SO answer. It turns out there is a builtin rot13 encoder in python in the codecs module :

# Python 2 & 3 compatible
import codecs
rot13 = lambda s : codecs.getencoder("rot-13")(s)[0]

# Answer
rot13("Gur dhvpx oebja sbk whzcrq bire gur ynml qbt")

Update 2 : Since the OP is adamant to know the answer and does not seem to have python installed, I have a created JS solution https://codepen.io/anon/pen/zPYVQP. Others please proceed with caution.

How to ROT13 encode in Python3?

Aha! I thought it had been dropped from Python 3, but no - it is just that the interface has changed, because a codec has to return bytes (and this is str-to-str).

This is from http://www.wefearchange.org/2012/01/python-3-porting-fun-redux.html :

import codecs
s = "hello"
enc = codecs.getencoder( "rot-13" )
os = enc( s )[0]

Short Rot (N) decode function in python

You don't have to recreate anything. Just shift the letters in the other direction, i.e. instead of rot_encode(13), call rot_encode(-13) to decode the previously encoded string.

x = rot_encode(13)('Hello World')
y = rot_encode(-13)(x)
print(x) # Uryyb Jbeyq
print(y) # Hello World

Of course, you can also wrap this into a rot_decode function if you prefer.

def rot_decode(n):
return rot_encode(-n)

When doing a ROT13 manipulation why would you stagger the maketrans and not enter each alphabet sequentially?

There is no difference in output: both produce a dictionary. Now since the input is a bit different, the dictionary can be different as well. But dictionaries are not ordered in Python, so that means no one can make assumptions with that one. Both produce the same dictionary:

>>> str.maketrans( 
... "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz",
... "NOPQRSTUVWXYZABCDEFGHIJKLMnopqrstuvwxyzabcdefghijklm")
{65: 78, 66: 79, 67: 80, 68: 81, 69: 82, 70: 83, 71: 84, 72: 85, 73: 86, 74: 87, 75: 88, 76: 89, 77: 90, 78: 65, 79: 66, 80: 67, 81: 68, 82: 69, 83: 70, 84: 71, 85: 72, 86: 73, 87: 74, 88: 75, 89: 76, 90: 77, 97: 110, 98: 111, 99: 112, 100: 113, 101: 114, 102: 115, 103: 116, 104: 117, 105: 118, 106: 119, 107: 120, 108: 121, 109: 122, 110: 97, 111: 98, 112: 99, 113: 100, 114: 101, 115: 102, 116: 103, 117: 104, 118: 105, 119: 106, 120: 107, 121: 108, 122: 109}
>>> str.maketrans(
... "ABCDEFGHIJKLMabcdefghijklmNOPQRSTUVWXYZnopqrstuvwxyz",
... "NOPQRSTUVWXYZnopqrstuvwxyzABCDEFGHIJKLMabcdefghijklm")
{65: 78, 66: 79, 67: 80, 68: 81, 69: 82, 70: 83, 71: 84, 72: 85, 73: 86, 74: 87, 75: 88, 76: 89, 77: 90, 78: 65, 79: 66, 80: 67, 81: 68, 82: 69, 83: 70, 84: 71, 85: 72, 86: 73, 87: 74, 88: 75, 89: 76, 90: 77, 97: 110, 98: 111, 99: 112, 100: 113, 101: 114, 102: 115, 103: 116, 104: 117, 105: 118, 106: 119, 107: 120, 108: 121, 109: 122, 110: 97, 111: 98, 112: 99, 113: 100, 114: 101, 115: 102, 116: 103, 117: 104, 118: 105, 119: 106, 120: 107, 121: 108, 122: 109}

The reason I think is that it is easier to verify for the human eye that the string is indeed shifted 13 positions. The alphabet contains 26 characters, and it is halfway between 'm' and 'n'. If we write:

rot13 = string.maketrans( 
"ABCDEFGHIJKLMabcdefghijklmNOPQRSTUVWXYZnopqrstuvwxyz",
"NOPQRSTUVWXYZnopqrstuvwxyzABCDEFGHIJKLMabcdefghijklm")

Notice the characters put in boldface. One can see that where the character sequence on the first line ends, it continues on the second line and vice versa. So here we have 2 points in the source code where we can easily check that we are still correct. This is easier than calculating 13 places forward and backward.



Related Topics



Leave a reply



Submit