티스토리 뷰

반응형

Stack Overflow에 자주 검색, 등록되는 문제들과 제가 개발 중 찾아 본 문제들 중에서 나중에도 찾아 볼 것 같은 문제들을 정리하고 있습니다.

Stack Overflow에서 가장 먼저 확인하게 되는 가장 높은 점수를 받은 Solution과 현 시점에 도움이 될 수 있는 가장 최근에 업데이트(최소 점수 확보)된 Solution을 각각 정리하였습니다.

 

아래 word cloud를 통해 이번 포스팅의 주요 키워드를 미리 확인하세요.

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

"this" 모듈의 소스 코드는 무엇을 하고 있습니까?

 문제 내용 

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

파이썬 인터프리터를 열고 "import this"를 입력하면 다음과 같이 출력됩니다.

 

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:

파이썬 소스(Lib/this.py)에서 이 텍스트는 흥미로운 코드 조각에 의해 생성된다.

 

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])

 

 

 높은 점수를 받은 Solution 

This is called rot13 encoding:

이것을 rot13 인코딩이라고 한다:

 

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.

대문자(65의 용도)와 소문자(97의 용도) 모두에 대해 변환 테이블을 작성합니다.

 

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

Prints the translated string.

변환된 문자열을 인쇄합니다.

 

 

 

 가장 최근 달린 Solution 

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

ROT13 인코딩을 사용합니다. 이것은 농담이기 때문에 사용된다.

 

You can also use Python functions to decode string.

또한 Python 함수를 사용하여 문자열을 디코딩할 수 있습니다.

 

Python 2 only:

파이썬 2만 해당:

 

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

Python 2 & 3:

파이썬 2 및 3:

 

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

 

 

출처 : https://stackoverflow.com/questions/5855758/what-is-the-source-code-of-the-this-module-doing

반응형
댓글
공지사항
최근에 올라온 글