美文网首页
pythonchallenge 闯关心得(更新至#3)

pythonchallenge 闯关心得(更新至#3)

作者: 清脆de琉璃 | 来源:发表于2017-10-23 01:03 被阅读0次

    看到一个很有趣的python游戏解谜网站。
    http://www.pythonchallenge.com/
    每个小谜题都考验了python的一些典型应用和方法。尝试解出一些题,并在此记录解题心得。
    欢迎对代码或者方法进行交流。

    level 0

    一个warm up练习。刚接触的时候一脸懵逼,不知道怎么写。图中的显示器让你计算2的38次方,在hint提示中让你修改url
    所以计算出2的38次方之后(2**38),把答案改写到url中即可过关。

    level 1

    一开始认为图片中标记的是一种字母的映射关系。看到待解的string之后,单纯的映射三个字母依然很杂乱。
    k, l, m, o, p, q, e, f, g 我们发现字母都是要在原字母上面+2.
    在处理的时候,string无法做转换字符的加法,需要先将string转换为ASCII,操作之后再转换回string.
    ASCII 转 字符: chr()
    字符转换为ASCIIord()

    >>> "".join(map(lambda x: chr(ord(x) + 2), "map"))
    'ocr'
    

    题目中让解出url中的"map",写出ocr即可通过。

    题目中另有一个长串字符串,可以看看如何处理。

    s = "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."
    

    如果用上面的处理方法,得到数据并不准确。

    >>> "".join(map(lambda x: chr(ord(x) + 2), s))
    'i"hope"you"didnt"tr{nsl{te"it"|y"h{nd0"th{ts"wh{t"computers"{re"for0"doing"it"in"|y"h{nd"is"inefficient"{nd"th{t)s"why"this"text"is"so"long0"using"string0m{ketr{ns*+"is"recommended0"now"{pply"on"the"url0'
    

    原因是所有的字符都+2,小写字母y, z之后,就是别的符号了。需要做循环处理。并且原本就是符号的信息需要保留。

    def convert_char(ch):
        if 'x' < ch <= 'z':
            return chr(ord(ch) - 26 + 2)
        elif 'a' <= ch <= 'x':
            return chr(ord(ch) + 2)
        else:
            return ch
    
    >>> "".join(map(convert_char, s))
    "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."
    

    level 2

    根据提示,从浏览器打开 view page source code. 能看到网页下面有一大串注释的代码。里面有提示让你从乱的符号中找出来字符。
    把字符存成string,利用正则表达式查找字符。

    s = """
    乱码字符串
    """
    print re.findall('[A-Z]+|[a-z]+', s)
    ['e', 'q', 'u', 'a', 'l', 'i', 't', 'y']
    

    在url中输入 equality 即可过关

    level 3

    还是需要查看网页源码,其中有大串注释信息。
    根据图下面的提示,需要找到中间一个小写字母,两旁各有三个大写字母。这里需要注意,三个大写字母之后,还应该是小写字母。如 aAAAaAAAa, 需要找到中间那个a

    print re.findall('[a-z]{1}[A-Z]{3}([a-z]{1})[A-Z]{3}[a-z]{1}', s)
    ['l', 'i', 'n', 'k', 'e', 'd', 'l', 'i', 's', 't']
    

    在url中输入 linkedlist会得到新的提示,把.html改成.php即可过关

    相关文章

      网友评论

          本文标题:pythonchallenge 闯关心得(更新至#3)

          本文链接:https://www.haomeiwen.com/subject/obcouxtx.html