Python Challenge游戏攻略(一)

   

Python Challenge是一个古老的网页闯关游戏,为程序员设计,一共33关,只有在一关通过之后,才能获得会提供下一关挑战的网页地址,以此类推到最终关。蛮有意思的,自己做了13道,后面就没坚持下去,做个记录。

0.python的指数函数

计算2的38次方:

1,math.pow(2,38)这个内置函数 得到274877906944,输入url中即可
2,**运算符

这两种方法效果是一样的,但是当

b为分数,a为负数:当幂运算符的底数为负数、幂为分数时,Python会抛出ValueError: negative number cannot be raised to a fractional power异常,

这时有两种方法可以解决此问题:

* 1、底数a正负均可不影响你的算法的话,加一个绝对值就行了即abs(a);

* 2、如果底数a必须是正、或必须是负的话,需要采用复数进行运算。因此凡是遇到幂为分数的项,都将底数用complex()转换为复数。

1.python中的替换

不只是是哪个字母,而是所有的,替换为他的next .next()
翻译出来是:

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.

做法:

# coding:utf-8
#需要导入maketrans
from string import maketrans
a = “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.”
c = “abcdefghijklmnopqrstuvwxyz”
d = “cdefghijklmnopqrstuvwxyzab”
trantab = maketrans(c, d)
print(a.translate(trantab))
# 需要安装
pip install python-tr
from tr import tr
>>> tr('bn','cr','bunny')'curry'
最后将url中的map使用同样的方法做转换:
It tells me to apply the same translation on the URL: ‘map’, which gives ‘ocr’. Therefore the url for level 2 is here.
第二题地址: http://www.pythonchallenge.com/pc/def/ocr.html

2.识别字符

识别字符,有可能在书里,也有可能在网页源码里面:
find rare characters in the mess below:找出下面源码中的稀有字母

re.compile('<!--((?:[^-]+|-[^-]|--[^>])*)-->', re.S).findall(src)[-1]

import re
a=”源码”
print ”.join(re.findall(‘[a-z]’, a))
得到:equality

3.正则表达式

又是正则表达式:拿到源码中的字符串

print ”.join(re.findall(‘[^A-Z][A-Z]{3}([a-z])[A-Z]{3}[^A-Z]’, text))
# 得到
linkedlist

4.爬取页面元素相关

点击图片:http://www.pythonchallenge.com/pc/def/linkedlist.php?nothing=12345
提示:and the next nothing is 44827
输入第三个的时候:Your hands are getting tired and the next nothing is 94485
嘲讽。。。。
找到没有下一个的网页url即可

def next_page(p):
    url = ‘http://www.pythonchallenge.com/pc/def/linkedlist.php?nothing=‘ + str(p)
    text = requests.get(url).content
    a = text.find(‘and the next nothing is’)
    if a == -1:
        print(text)
        print(p)
    else:
        b = text.split(“is “)[1]
        print(b)
        return b
p = 12345
for i in range(100):
    p = next_page(p)
Yes. Divide by two and keep going.
16044
除以2 得到 8022
There maybe misleading numbers in the text. One example is 82683. Look only for the next nothing and the next nothing is 63579
p = 63579
for i in range(200):
    p = next_page(p)
————–
66831
peak.html

5.pickle模块

提示0:画面上是一座小山,对应页面标题是peak hell。 提示1:读出来。 提示2:peak hell听起来像什么?

这个难为英语不好的人了,不过如果对Python熟悉的话,会知道一个叫pickle的东西。试着访问pickle.html,得到了肯定yes! pickle!。

# coding:utf-8
from string import maketrans
import urllib
import pickle
url = ‘http://www.pythonchallenge.com/pc/def/banner.p‘
lines = urllib.urlopen(url).readlines()
data = pickle.loads( ”.join(lines) )
for row in data:
    result = ”
    for chars in row:
        result += chars[0] * chars[1]
    print result
  • 官网链接:http://www.pythonchallenge.com/
  • 参考博客:http://www.cnblogs.com/jimnox/archive/2009/12/08/tips-to-python-challenge.html
  • 参考:http://garethrees.org/2007/05/07/python-challenge/

Written by

说点什么

欢迎讨论

avatar

此站点使用Akismet来减少垃圾评论。了解我们如何处理您的评论数据

  Subscribe  
提醒