Python Challenge(0--1关)——我的解题报告(running with python3.x)

这个编程游戏,我现在开始在我的博客记录我一关一关刷下去的日子。另外说一句,我用的是python版本是3.3.2

第一关:http://www.pythonchallenge.com/pc/def/0.html

        这关很简单,图片给出的线索就是求2的38次方。因为python的“科学计算”能力,所以我们直接把结果搞出来就可以了。

print(2**38)
        根据暗示,修改url。看一下url,估计把0换成结果274877906944就好了。果然,经过一次重定向,成功。

第二关:http://www.pythonchallenge.com/pc/def/map.html

        看一下这关,一个图片,加上一段话。分析一下图片,很容易地就可以找到规律。字母变换就好了,比如将K变成M,就是变成它后面的第二个字母。那么下面的一段话“翻译”过来就可以得到更进一步的东西了。这里面有一些注意的东西,比如z->b,标点符号不变。简单的想,可以通过ascii码。

        先写一段程序来看看:

source='''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.'''

ans = ''
for c in source:
    if c.isalpha():
        ans+= chr( ((ord(c)-ord('a'))+2)%26 + ord('a'))
    else:
        ans += c
print(ans)
        上面这段程序就可以翻译出结果了。结果是:
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,发现应该是按照这个规律把map映射一下就可以了。下一关的网址呼之欲出。

        不过我们应该更加有探索精神,上面那段话提示我们用string.maketrans()处理这关。那么这是什么呢?

        pythonchallenge的提示很有可能是基于py2k的。果然我在交互式命令下导入string模块再help一下是没有的。那么考虑到2->3的一些变化,很有可能变为字符串内建方法了。果然,help(''.maketrans)一下就出现了(注意不要打成help(''.maketrans()))。

>>> help(''.maketrans)
Help on built-in function maketrans:

maketrans(...)
    str.maketrans(x[, y[, z]]) -> dict (static method)
    
    Return a translation table usable for str.translate().
    If there is only one argument, it must be a dictionary mapping Unicode
    ordinals (integers) or characters to Unicode ordinals, strings or None.
    Character keys will be then converted to ordinals.
    If there are two arguments, they must be strings of equal length, and
    in the resulting dictionary, each character in x will be mapped to the
    character at the same position in y. If there is a third argument, it
    must be a string, whose characters will be mapped to None in the result.
        上面的话是很容易读懂的。不过我们发现,还需要一个str,translate()函数。那这又是什么呢?再help一下。

>>> help(''.translate)
Help on built-in function translate:

translate(...)
    S.translate(table) -> str
    
    Return a copy of the string S, where all characters have been mapped
    through the given translation table, which must be a mapping of
    Unicode ordinals to Unicode ordinals, strings, or None.
    Unmapped characters are left untouched. Characters mapped to None
    are deleted.
        看上面的话就会读明白了,就是先通过maketrans创建一张表示映射关系的表(想一想,这又给我们什么提示?)。然后调用translate函数就可以。

        在做这个之前,我想到我学习python时书上有提过,python内建了一些列表,比如小写字母表等。查了下官方文档,http://docs.python.org/3.3/library/string.html里面的东西一定会对你有帮助。

        那下面就是这个方法的代码:

source='''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.'''
from string import ascii_lowercase
ret = ''.maketrans(ascii_lowercase, ascii_lowercase[2:]+ascii_lowercase[:2])
ans = source.translate(ret)
print(ans)
        这个是按照关卡建议的方法过的题。不过按照这个提示,很容易想到自己手动构建一个映射关系。这个关系可以由字典表示。代码如下,省去了那段给出的内容。

from string import ascii_lowercase, punctuation, whitespace
trans = dict( zip(ascii_lowercase+punctuation+whitespace,ascii_lowercase[2:]+ascii_lowercase[:2]+punctuation+whitespace))
##print(trans)
ans = ""
for c in source:
    ans += trans[c]
print(ans)
        上面的代码通过,但是有一点要注意,whitespace也要加进去并建立映射关系。
        好了,这关玩完了,那么下一关map->ocr,所以url是: http://www.pythonchallenge.com/pc/def/ocr.html

Be more pythonic!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值