import this

Pythonインタプリタで打ち込むと、Pythonの設計哲学が出てくる例のアレ。

import this

いわゆるThe Zen of PythonというPythonユーザーならお馴染み?の機能ですが、じゃあこれ、dir(this)したらどうなるのっということで実行してみたら…

>>> dir(this)
['__builtins__', '__doc__', '__file__', '__name__', '__package__', 'c', 'd', 'i', 's']

this.c this.d this.i this.s
なる4つの変数を発見。

それぞれprintさせてみると…

>>> print this.c
!
>>> print this.d
{'A': 'N', 'C': 'P', 'B': 'O', 'E': 'R', 'D': 'Q', 'G': 'T', 'F': 'S', 'I': 'V', 'H': 'U', 'K': 'X', 'J': 'W', 'M': 'Z', 'L': 'Y', 'O': 'B', 'N': 'A', 'Q': 'D', 'P': 'C', 'S': 'F', 'R': 'E', 'U': 'H', 'T': 'G', 'W': 'J', 'V': 'I', 'Y': 'L', 'X': 'K', 'Z': 'M', 'a': 'n', 'c': 'p', 'b': 'o', 'e': 'r', 'd': 'q', 'g': 't', 'f': 's', 'i': 'v', 'h': 'u', 'k': 'x', 'j': 'w', 'm': 'z', 'l': 'y', 'o': 'b', 'n': 'a', 'q': 'd', 'p': 'c', 's': 'f', 'r': 'e', 'u': 'h', 't': 'g', 'w': 'j', 'v': 'i', 'y': 'l', 'x': 'k', 'z': 'm'}
>>> print this.i
25
>>> print this.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!

んんんんん?The Zen of Pythonはどこいった?
なんてボケはナシとして、感の良い読者ならこれは暗号化されている!と一目見て思ったハズ。
遊び心満載のimport thisに心惹かれた僕はPythonの禅を再生しようと挑戦してみることに。

まずはthis.sが暗号化された文で、this.dが恐らくは置換表だろうと踏み、
じゃあthis.sをforで回して1文字づつ置換させればいいんじゃねっと言うことでできたのが次のコード。

for i in this.s:
	print this.d[i]

実行結果は…

T
h
e
Traceback (most recent call last):
  File "<pyshell#25>", line 2, in <module>
    print this.d[i]
KeyError: ' '

そりゃそーだ、スペースは置換表に無いだろうし、無ければエラー出る。しかも1文字ごとに改行されてるし!
ということで書きなおしたのが以下。

>>> for i in this.s:
	print this.d.get(i,i),

	
T h e   Z e n   o f   P y t h o n ,   b y   T i m   P e t e r s 

B e a u t i f u l   i s   b e t t e r   t h a n   u g l y . 
E x p l i c i t   i s   b e t t e r   t h a n   i m p l i c i t . 
S i m p l e   i s   b e t t e r   t h a n   c o m p l e x . 
C o m p l e x   i s   b e t t e r   t h a n   c o m p l i c a t e d . 
F l a t   i s   b e t t e r   t h a n   n e s t e d . 
S p a r s e   i s   b e t t e r   t h a n   d e n s e . 
R e a d a b i l i t y   c o u n t s . 
S p e c i a l   c a s e s   a r e n ' t   s p e c i a l   e n o u g h   t o   b r e a k   t h e   r u l e s . 
A l t h o u g h   p r a c t i c a l i t y   b e a t s   p u r i t y . 
E r r o r s   s h o u l d   n e v e r   p a s s   s i l e n t l y . 
U n l e s s   e x p l i c i t l y   s i l e n c e d . 
I n   t h e   f a c e   o f   a m b i g u i t y ,   r e f u s e   t h e   t e m p t a t i o n   t o   g u e s s . 
T h e r e   s h o u l d   b e   o n e - -   a n d   p r e f e r a b l y   o n l y   o n e   - - o b v i o u s   w a y   t o   d o   i t . 
A l t h o u g h   t h a t   w a y   m a y   n o t   b e   o b v i o u s   a t   f i r s t   u n l e s s   y o u ' r e   D u t c h . 
N o w   i s   b e t t e r   t h a n   n e v e r . 
A l t h o u g h   n e v e r   i s   o f t e n   b e t t e r   t h a n   * r i g h t *   n o w . 
I f   t h e   i m p l e m e n t a t i o n   i s   h a r d   t o   e x p l a i n ,   i t ' s   a   b a d   i d e a . 
I f   t h e   i m p l e m e n t a t i o n   i s   e a s y   t o   e x p l a i n ,   i t   m a y   b e   a   g o o d   i d e a . 
N a m e s p a c e s   a r e   o n e   h o n k i n g   g r e a t   i d e a   - -   l e t ' s   d o   m o r e   o f   t h o s e !

置換には成功したけどなんじゃこりゃー!1文字ごとにスペースが挟まれてしまいました。
これはBeautifulでもReadabilityでもない!
諦めて素直にGoogle先生に聞くことに。
調べたところによると、文字列の置換にはtranslateとstring.maketransを使うのが良いのだそうな。
参照先のサイトを参考にコードをでっち上げようと

import this
import string
print this.s.translate(string.maketrans(…

ここまで書いて気がついた。
ディクショナリからキーや値のリストを取り出す方法 -- .keys()や.values() -- は知ってるけど、連結させるのってどうするんだ!?
まさか

>>> str = ""
>>> a = {"foo":"bar","spam":"yum"}
>>> a
{'foo': 'bar', 'spam': 'yum'}
>>> print a.keys()
['foo', 'spam']
>>> for i in a.keys():
	str += i
>>> print str
foospam

とか超絶スマートじゃないし、助けてGoogle先生
すると、.join()さんが助けてくれるという文面を発見。

>>> a = {"foo":"bar","spam":"yum"}
>>> a
{'foo': 'bar', 'spam': 'yum'}
>>> print "".join(a.keys())
foospam
>>> print "".join(a.values())
baryum

ワーオ、素晴らしい。後はこれを先ほどのコードに当てはめれば…

>>> import this
>>> import string
>>> print this.s.translate(string.maketrans("".join(this.d.keys()),"".join(this.d.values()))

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!

できた!
こうしてまた1つPythonの階段を登った気分になりました。

解いた後に他の解法を調べていると、どうやらthis.sは平文にROT13なる有名かつ単純な暗号化をしているだけのようで、
Pythonバージョン2.x.x以上に搭載されている.decode("rot13")でカンタンに復号化できてしまうそうです。

print this.s.decode("rot13")

しかし、こんな解法があるにも関わらず敢えてthis.dを作っておいたということは、恐らくは上に書いたようなコードを試して欲しいという開発者からのメッセージだったような気がしますね。
では今日はこのへんで。