很早就看到了http://www.pythonchallenge.com/,一直沒往下做,後來發現還是蠻好玩的,所以在這裡記錄一下,希望能堅持走到最後的level。
Level 0
題目:238(http://www.pythonchallenge.com/pc/def/0.html)
題意:熱身題,最簡單的一道了吧,本意是熟悉挑戰規則,即將答案替換掉當前url裡的0.html
解謎:
1 print 2<<(38-1)
2 print pow(2,38)
可以用內建函數或者乾脆移位操作,移位時要注意是38-1,因為題目是2乘以37個2,所以……不解釋
Level 1
題目:http://www.pythonchallenge.com/pc/def/map.html
題意: 觀察圖中的映射規則,推斷出應該是按照這種規則來翻譯圖下面的那句話,無論如何,先翻譯一下吧。
解謎:
1 str = "g fmnc wms bgblr rpylqjyrc gr zw fylb. rfyrq ufyr amknsrcpq ypc dmp. bmgle gr gl zw fylb gq glcddgagclr ylb rfyr'q ufw rfgq rcvr gq qm jmle. sqgle qrpgle.kyicrpylq() gq pcamkkclbcb. lmu ynnjw ml rfc spj."
2 for c in str:
3 if not c>='a' and c<="z":
4 sys.stdout.write(c),
5 continue
6 if c == 'y':
7 print 'a',
8 elif c == 'z':
9 print 'b',
10 else:
11 sys.stdout.write(chr(ord(c)+2))
很挫, 翻譯後得知:
i hope you didnt translate it by hand. thats what computers are for. doing it in by hand is inefficient and that's why this text is so long. using string.maketrans() is recommended. now apply on the url.i hope you didnt translate it by hand. thats what computers are for. doing it in by hand is inefficient and that's why this text is so long. using string.maketrans() is recommended. now apply on the url.
大意就是,按照相同的規則對當前url進行翻譯,得出下一題的url。不過題目推薦了maketrans(),筆者對python也是個半瓶水,很多api不熟,翻了翻文檔,於是決定用最python的方式再來一次:
1 import string
2 table = string.maketrans(string.ascii_lowercase,"cdefghijklmnopqrstuvwxyzab");
3 print string.translate(str, table)
4 print string.translate("http://www.pythonchallenge.com/pc/def/map.html",table)
得到:jvvr://yyy.ravjqpejcnngpig.eqo/re/fgh/ocr.jvon
那麼下一題應該就是:http://www.pythonchallenge.com/pc/def/ocr.html。
Python Challenge提供了參考代碼,可以將當前url裡的/pc改成/pcc來查看上一題的參考代碼,所以Level1的參考代碼可以查看:http://www.pythonchallenge.com/pcc/def/ocr.html
看了之後我發現我有一個地方確實不太簡潔,忘了python那無敵的切片了,特此修正:
1 table = string.maketrans(string.ascii_lowercase,string.ascii_lowercase[2:]+string.ascii_lowercase[:2])