What is the source code of the "this" module doing?

Python

Python Problem Overview


If you open a Python interpreter, and type "import this", as you know, it prints:

> The Zen of Python, by Tim Peters
> > Beautiful is better than ugly.
> Explicit is better than implicit.
> Simple is better than complex.
> Complex is better than complicated.
> Flat is better than nested.
> Sparse is better than dense.
> Readability counts.
> Special cases aren't special enough to break the rules.
> Although practicality beats purity.
> Errors should never pass silently.
> Unless explicitly silenced.
> In the face of ambiguity, refuse the temptation to guess.
> There should be one-- and preferably only one --obvious way to do it.
> Although that way may not be obvious at first unless you're Dutch.
> Now is better than never.
> Although never is often better than right now.
> If the implementation is hard to explain, it's a bad idea.
> If the implementation is easy to explain, it may be a good idea.
> Namespaces are one honking great idea -- let's do more of those!

In the python source(Lib/this.py) this text is generated by a curious piece of code:

s = """Gur Mra bs Clguba, ol Gvz Crgref

Ornhgvshy vf orggre guna htyl.
Rkcyvpvg vf orggre guna vzcyvpvg.
Fvzcyr vf orggre guna pbzcyrk.
Pbzcyrk vf orggre guna pbzcyvpngrq.
Syng vf orggre guna arfgrq.
Fcnefr vf orggre guna qrafr.
Ernqnovyvgl pbhagf.
Fcrpvny pnfrf nera'g fcrpvny rabhtu gb oernx gur ehyrf.
Nygubhtu cenpgvpnyvgl orngf chevgl.
Reebef fubhyq arire cnff fvyragyl.
Hayrff rkcyvpvgyl fvyraprq.
Va gur snpr bs nzovthvgl, ershfr gur grzcgngvba gb thrff.
Gurer fubhyq or bar-- naq cersrenoyl bayl bar --boivbhf jnl gb qb vg.
Nygubhtu gung jnl znl abg or boivbhf ng svefg hayrff lbh'er Qhgpu.
Abj vf orggre guna arire.
Nygubhtu arire vf bsgra orggre guna *evtug* abj.
Vs gur vzcyrzragngvba vf uneq gb rkcynva, vg'f n onq vqrn.
Vs gur vzcyrzragngvba vf rnfl gb rkcynva, vg znl or n tbbq vqrn.
Anzrfcnprf ner bar ubaxvat terng vqrn -- yrg'f qb zber bs gubfr!"""

d = {}
for c in (65, 97):
    for i in range(26):
        d[chr(i+c)] = chr((i+13) % 26 + c)

print "".join([d.get(c, c) for c in s])

Python Solutions


Solution 1 - Python

This is called rot13 encoding:

d = {}
for c in (65, 97):
    for i in range(26):
        d[chr(i+c)] = chr((i+13) % 26 + c)

Builds the translation table, for both uppercase (this is what 65 is for) and lowercase (this is what 97 is for) chars.

print "".join([d.get(c, c) for c in s])

Prints the translated string.

Solution 2 - Python

If you want to make the ROT13 substitution by hand - or in your head - you can check that because 13*2 = 26 (the number of the letters of the English alphabet), it's essentially an interchange:

a <-> n
b <-> o
c <-> p
...
m <-> z

A <-> N
B <-> O
C <-> P
...
M <-> Z 

Vs lbh cenpgvfr ybat rabhtu, lbh'yy riraghnyyl znfgre gur Mra bs EBG-13 nytbevguz naq ernq guvf Xyvatba ybbxvat grkgf jvgubhg pbzchgre uryc.

Solution 3 - Python

It uses ROT13 encoding. This is used because it's a joke.

You can also use Python functions to decode string.

Python 2 only:

import this
print(this.s.decode('rot13'))

Python 2 & 3:

import codecs
print(codecs.decode(this.s, 'rot-13'))

Solution 4 - Python

It's a substitution cipher, rot13.

Solution 5 - Python

It's a substitution cipher (as mentioned in previous answers). Historically speaking, it's the Caesar cipher.

https://www.google.de/search?q=caesar+cipher&cad=h

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionbyterussianView Question on Stackoverflow
Solution 1 - PythonEli BenderskyView Answer on Stackoverflow
Solution 2 - PythonypercubeᵀᴹView Answer on Stackoverflow
Solution 3 - PythontrapwalkerView Answer on Stackoverflow
Solution 4 - PythonMichael J. BarberView Answer on Stackoverflow
Solution 5 - PythonCristobal CardonaView Answer on Stackoverflow