zoukankan      html  css  js  c++  java
  • Python Challenge 第四关

    进入了第四关。只有一张图,我还是像往常一样查看源代码。果然,发现了一行注释:urllib may help. DON'T TRY ALL NOTHINGS, since it will never end. 400 times is more than enough.

    看来这一关是和网络编程有关。只是没明白这个 NOTHINGS 是个什么东西。回到原页面,发现这张图是个链接,于是就点了进去,然后出现了只有一句话的页面:

    and the next nothing is 44827

    查看了下源代码,也是只有这一句,这回没注释了。百思不得其解的时候,注意到这个页面的 url 变成了:

    http://www.pythonchallenge.com/pc/def/linkedlist.php?nothing=12345

    后面出现了个 nothing=12345,加上刚出现的句子,就把 12345 改成 44827,果然出现了下一个页面,得到 next nothing is 45439,这下知道原来 nothing 就是则个 url 后面的数字。再改成 45439,这下页面多了一句:

    Your hands are getting tired and the next nothing is 94485

    看来这一句是提醒我不要再用手试下去了,手累了-_-|||。

    这时候想起刚才的注释,提示使用 urllib,并且最多400次,就知道应该写个循环来做,提取出每次页面中的数字,并更改下一次访问的 url。提取数字可以用正则表达式,也可以直接把字符串分成列表,取它的最后一项。

        url = 'http://www.pythonchallenge.com/pc/def/linkedlist.php?nothing='
        nothing = '12345'
        for i in range(400):
            rst = urllib.urlopen(url + nothing).read()
            rstList = rst.split(' ')
            nothing = [item for item in rstList if item.isdigit()][0]
            print '%d: %s' % (i, nothing)

    这样跑到80多次的时候 nothing = 16044,出现了错误,提示 nothing = [...] 这一句出现了 out of range。也就是这时候的列表为空。我以为到底了,就把 url 结尾改成 16044,进入页面一看,是这么一句:

    Yes. Divide by two and keep going.

    我就把 16044 除以 2,得到 8022,改了 url 继续,得到跟之前的一样,提示下一个 nothing 是多少。看来这一句明显是找茬啊。。。把上面的代码中第一个nothing 值改成 8022,在第250次的时候又出错,这时 nothing 值为 66831,输入一看,得到结果了:peak.html。

    为了让整个过程自动化,修改上面的代码,让代码自动判断并且不会报错:

        url = 'http://www.pythonchallenge.com/pc/def/linkedlist.php?nothing='
        nothing = '12345'
        for i in range(400):
            rst = urllib.urlopen(url + nothing).read()
            if rst.startswith('Yes'):
                nothing = str(int(nothing) / 2)
                continue
            rstList = rst.split(' ')
            numList = [item for item in rstList if item.isdigit()]
            if len(numList) != 0:
                nothing = numList[0]
                print '%d: %s' % (i, nothing)
            else:
                print rstList[0]
                break

    这样再跑一次,程序就可以得到正确的结果了。将 url 后面改为 peak.html,进入第五关:http://www.pythonchallenge.com/pc/def/peak.html

  • 相关阅读:
    ElementUI中弹窗使用textarea原样显示SpringBoot后台带换行的StringBuilder内容
    Node搭建静态资源服务器时后缀名与响应头映射关系的Json文件
    Nodejs中搭建一个静态Web服务器,通过读取文件获取响应类型
    JS中怎样比较两个时分格式的时间大小
    ElementUI中对el-table的某一列的时间进行格式化
    MongoDb在Windows上的下载安装以及可视化工具的下载与使用
    Express中使用ejs新建项目以及ejs中实现传参、局部视图include、循环列表数据的使用
    FFmpeg-20160506-snapshot-bin
    FFmpeg-20160428-snapshot-bin
    FFmpeg-20160422-snapshot-bin
  • 原文地址:https://www.cnblogs.com/dukeleo/p/3459973.html
Copyright © 2011-2022 走看看