Python Challenge解谜手记 Level0-1

很早就看到了http://www.pythonchallenge.com/,一直没往下做,后来发现还是蛮好玩的,所以在这里记录一下,希望能坚持走到最后的level。

Level 0

题目:238http://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])

 

 

posted on 2010-05-29 18:12  晋哥哥  阅读(3448)  评论(7编辑  收藏  举报

导航