python challenge 0-4

在知乎上看到有人推荐这个小游戏  刚好拿来学习python      http://www.pythonchallenge.com/pc/def/0.html

第0关  2的38次方

2**38

把0改成274877906944进入第1关

第1关 一个字符串转换  把每个字母右移2  

s=raw_input()
y=''
for i in range(0,len(s)):
    if s[i].isalpha():
        if s[i] not in 'yz':
           y+=chr(ord(s[i])+2)
        else:
            y+=chr(ord(s[i])+2-26)
    else :
        y+=s[i]
print y

处理下面的字符串得到提示文字: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.

string.maketrans() 还没学过 一会去看看

将map转换为ocr进入下一关

第2关 查看源代码 , 发现提示find rare characters in the mess below:

import urllib2
url=urllib2.urlopen("http://www.pythonchallenge.com/pc/def/ocr.html")
a=''
str=url.readlines()
for i in range(38,len(str)):
   for i in str[i]:
      if i.isalpha():
          a+=i
print a
url.close()
import urllib2
url=urllib2.urlopen("http://www.pythonchallenge.com/pc/def/ocr.html")
a=''
for i in range(37):
    url.readline()
str=url.read()
for i in str:
    if i.isalpha():
        a+=i
print a
url.close()
</span>

速度都好慢    过程中学到的:readline和readlines,read都是用来读取文件内容,readline()每次读取一行,当前位置移到下一行;readlines()读取整个文件所有行,保存在一个列表(list)变量中,每行作为一个元素;read(size)从文件当前位置起读取size个字节(如果文件结束,就读取到文件结束为止),如果size是负值或省略,读取到文件结束为止,返回结果是一个字符串。readline()每次只读取一行,通常比 .readlines()慢得多。仅当没有足够内存可以一次读取整个文件时,才应该使用.readline()。   


算出结果equality进入第3关

第3关 One small letter, surrounded by EXACTLY three big bodyguards on each of its sides.
在源代码里面找到两边恰好有三个大写字母的小写字母

先学习了一下正则表达式: http://blog.csdn.net/jiangjieqazwsx/article/details/45622163

import urllib2
import re
s=r"[^A-Z][A-Z]{3}[a-z][A-Z]{3}[^A-Z]"
url=urllib2.urlopen("http://www.pythonchallenge.com/pc/def/equality.html")
for i in range(21):
    url.readline()
str=url.read()
x= re.findall(s,str)
result=''
for i in range(len(x)):
    result+=x[i][4]
print result
url.close()
算出结果linkedlist 进入第4关

第4关,不停地跳转   开始尝试了以下手动   完全看不到希望  看了一下源代码  <!-- urllib may help. DON'T TRY ALL NOTHINGS, since it will never 
 end. 400 times is more than enough. --> 

第一次的代码 

import urllib2
import re
s=r"[0-9]"
url1="http://www.pythonchallenge.com/pc/def/linkedlist.php?nothing="
nothing=raw_input()
count=0
url2=url1+nothing
url=urllib2.urlopen(url2)
str=url.read()
x= re.findall(s,str)
while x:
    count+=1
    nothing=''.join(x)
    print nothing
    url.close()
    url2=url1+nothing
    url=urllib2.urlopen(url2)
    str=url.read()
    x= re.findall(s,str)
print count

第一次运行得到16044  跳转85次  手动输入16044,得到提示Yes. Divide by two and keep going.。第二次输入8022,发现运行了好久都不停止 ,仔细看了下发现是个无限循环  ,令count<100,看了下什么时候开始无限循环的 ,直觉告诉我是nothing=8268363579 出问题了 因为nothing一下子大了好多,看了下前一个界面nothing=82682,果然提示There maybe misleading numbers in the text. One example is 82683. Look only for the next nothing and the next nothing is 63579。问题是我也不知道提示的格式,现在就希望一直是末尾吧。。修改后的程序如下:

import urllib2
import re
url1="http://www.pythonchallenge.com/pc/def/linkedlist.php?nothing="
nothing=raw_input()
count=0
url2=url1+nothing
url=urllib2.urlopen(url2)
str=url.read()
x= str.split()[-1]
while x and x.isdigit():
    count+=1
    nothing=''.join(x)
    print nothing
    url.close()
    url2=url1+nothing
    url=urllib2.urlopen(url2)
    str=url.read()
    x= str.split()[-1]
print count

输入8022 得到66831   跳转164次,手动输入nothing=66831  得到peak.html,进入第5关。85+164+3=252,也没有400次啊。。  



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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值