PythonChallenge Level 0~4

标签: pythonchallenge

PythonChallenge Level 0:    http://www.pythonchallenge.com/pc/def/0.html

    

 

    计算 2 的 38 次幂 将得到的结果替换URL中数字0

print 2**38

 

 

 

 

 

 

PythonChallenge Level 1:    http://www.pythonchallenge.com/pc/def/map.html


 

照笔记本中给出的规律解密图片下方给出的一串字符

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.
     用到两个函数

string.maketrans(fromto)

Return a translation table suitable for passing to translate(), that will map each character in from into the character at the same position in tofrom and to must have the same length.

##返回一个转换表 table( eg. 'A'-->'a', 'B'-->'b'), from 和 to 长度必须一致,在 from 中有的字符转换成 to 中对应位置的字符 

string.translate(stable[, deletechars])

Delete all characters from s that are in deletechars (if present), and then translate the characters using table, which must be a 256-character string giving the translation for each character value, indexed by its ordinal. If table is None, then only the character deletion step is performed.

##按照转换表 table,对字符串 s 进行转换

 

import string

#load the text 
text = '''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.
'''

#creat a translate table
table = string.maketrans(string.ascii_lowercase, string.ascii_lowercase[2:] + string.ascii_lowercase[:2])

#apply the translation table on string
print string.translate(text, table)

# Or you can "print text.translate(table)"

 if you don't know  "string.ascii_lowercase[2:]", "string.ascii_lowercase[:2]", "string.ascii_lowercase",  try to print it. 

 

执行结果: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 1 URL中 map 进行同样替换即可转至Level 2)

 

 

 

 

 

 

PythonChallenge Level 2:    http://www.pythonchallenge.com/pc/def/ocr.html

 

    

 

网页的左下角还有几条提示

General tips:

  • Use the hints. They are helpful, most of the times.
  • Investigate the data given to you.
  • Avoid looking for spoilers.

 We saw the hint “but MABYE they are in the page source” 

故执行: 右击网页 --> 查看网页源代码 

找到了这一关的谜题: find rare characters in the mess below:

 

用到一个 str.isalpha() 函数,判断是否字母

str.isalpha()

Return true if all characters in the string are alphabetic and there is at least one character, false otherwise.

For 8-bit strings, this method is locale-dependent.

 

import string

mess = ''' #copy the mess from Level.2's page source '''
ans = ''

for x in mess:
    if x.isaplha():
        ans += x

print ans

 

得到结果: equality

将其替换 Level 2 URL 中的 " ocr " 即转到level 3

 

 

 

 

 

PythonChallenge Level 3:    http://www.pythonchallenge.com/pc/def/equality.html


网页左下角内容

To see the solutions to the previous level, replace pc with pcc, i.e. go to: http://www.pythonchallenge.com/pcc/def/equality.html 

Join us on IRC: irc.freenode.net #pythonchallenge

网页左下角的内容与本题无关,但从句中'see the solutions to the previious level'中得到启发,同第二关一样查看了网页的源代码,其中果然给出了mess.

图片正下方提示

One small letter, surrounded by EXACTLY three big bodyguards on each of its sides.

一个小写字母两边都有三个大写字母,形如 XXXxXXX ,后经尝试并不能得到结果,搜索之,其他过关的人用来匹配的形式为 xXXXxXXXx ,虽然还没明白为何如此,但确实是到下一关的正确方向。

从给出的mess中找出xXXXxXXXx形式的字符,用到网页源代码获取模块Urllib2以及正则表达式re模块。

urllib2.urlopen(url[, data[, timeout[, cafile[, capath[, cadefault[, context]]]]])

Open the URL url, which can be either a string or a Request object.

data may be a string specifying additional data to send to the server, or None if no such data is needed. Currently HTTP requests are the only ones that use data; the HTTP request will be a POST instead of a GET when the data parameter is provided. data should be a buffer in the standard application/x-www-form-urlencoded format. The urllib.urlencode() function takes a mapping or sequence of 2-tuples and returns a string in this format. urllib2 module sends HTTP/1.1 requests with Connection:close header included.

The optional timeout parameter specifies a timeout in seconds for blocking operations like the connection attempt (if not specified, the global default timeout setting will be used). This actually only works for HTTP, HTTPS and FTP connections.

If context is specified, it must be a ssl.SSLContext instance describing the various SSL options. See HTTPSConnection for more details.

The optional cafile and capath parameters specify a set of trusted CA certificates for HTTPS requests. cafile should point to a single file containing a bundle of CA certificates, whereas capath should point to a directory of hashed certificate files. More information can be found in ssl.SSLContext.load_verify_locations().

The cadefault parameter is ignored.

This function returns a file-like object with three additional methods:

  • geturl() — return the URL of the resource retrieved, commonly used to determine if a redirect was followed
  • info() — return the meta-information of the page, such as headers, in the form of an mimetools.Message instance (see Quick Reference to HTTP Headers)
  • getcode() — return the HTTP status code of the response.

Raises URLError on errors.

Note that None may be returned if no handler handles the request (though the default installed global OpenerDirector uses UnknownHandlerto ensure this never happens).

In addition, if proxy settings are detected (for example, when a *_proxy environment variable like http_proxy is set), ProxyHandler is default installed and makes sure the requests are handled through the proxy.

Changed in version 2.6: timeout was added.

Changed in version 2.7.9: cafilecapathcadefault, and context were added.

 

re.findall(patternstringflags=0)

Return all non-overlapping matches of pattern in string, as a list of strings. The string is scanned left-to-right, and matches are returned in the order found. If one or more groups are present in the pattern, return a list of groups; this will be a list of tuples if the pattern has more than one group. Empty matches are included in the result unless they touch the beginning of another match.

New in version 1.5.2.

Changed in version 2.4: Added the optional flags argument.

 

import urllib2
import re
import string

text = urllib2.urlopen('http://www.pythonchallenge.com/pc/def/equality.html').read()
matstr = re.findall(r'[a-z][A-Z]{3}[a-z][A-Z]{3}[a-z]', text)
ans = ''
i = 0
while i < len(matstr):
    ans += matstr[i][4]
    i += 1
print ans








 

基于找出的匹配字符需有意义的角度,最后的ans只提取了xXXXxXXXx形式中间的小写字母

代码运行结果为 linkedlist 按照前几题的经验,替换Level 3中equality回车得到提示“linkedlist.php”,按提示再次替换html为php即转至Level 4

 

 

 

 

 

PythonChallenge Level 4:    http://www.pythonchallenge.com/pc/def/linkedlist.php

给了一幅没怎么看得懂的图,查看是否有隐藏提示于网页源代码中,右击-->查看网页源代码  如下

<html>
<head>
  <title>follow the chain</title>
  <link rel="stylesheet" type="text/css" href="../style.css">
</head>
<body>
<!-- urllib may help. DON'T TRY ALL NOTHINGS, since it will never 
end. 400 times is more than enough. -->
<center>
<a href="linkedlist.php?nothing=12345"><img src="chainsaw.jpg" border="0"/></a>
<br><br><font color="gold"></center>
Solutions to previous levels: <a href="http://wiki.pythonchallenge.com/"/>Python Challenge wiki</a>.
<br><br>
IRC: irc.freenode.net #pythonchallenge
</body>
</html>

 主题“follow the chain”,注释“ urllib may help. DON'T TRY ALL NOTHINGS, since it will never end. 400 times is more than enough.”,另分析源代码可知网页中图片添加了超链接herf="linkedlist.php?nothing=12345",返回点击图片得到文本提示"and the next nothing is 44827"。

综上分析明白了Level 4 题面要做的就是递归更换 网页图片超链接URL中的nothing值

import urllib2
import string

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

def get_Nothings(uid):
    if uid.isdigit():
        text = urllib2.urlopen(urLstr+uid).read()
        # print text
        nextuid = ''
        for x in text:
            if x.isdigit():
                nextuid += x 
        # print urLstr + uid             
        print urllib2.urlopen(urLstr+uid).read(),'\n'
        return get_Nothings(nextuid)
    else:
        pass
get_Nothings("12345") 

 代码运行一小段时间出现非数字给出nothing值的情况,提示“Yes,Divide by two and keep going”

将在其前的nothing值 16044 除以 2 即 8022 作为代码中函数get_Nothings()的初始值继续运行。

最后得到Level 4的结果 peak.html 将其替换到URL中即转至Level 5

Level 5 :http://www.pythonchallenge.com/pc/def/peak.html

Level 5~10 解题过程会记录在我的下一篇博客中,谢谢!-------by 江南

 

                

 
 

 

 

 

 

版权声明:本文为iteye_12924原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/iteye_12924/article/details/82619709

智能推荐

Python学习练习6----列表、字典的运用2

range 用法参见http://blog.csdn.net/chiclewu/article/details/50592368 直接在 在线编程工具中练习: https://www.tutorialspoint.com/execute_python_online.php 代码如下,增加range、列表的len()、字典的items()函数,for 函数也有了新变化 练习2: 2的运行结果,注意p...

PoolThreadCache

缓存构成   PoolThreadCache的缓存由三部分构成:tiny、small 和 normal。 tiny   缓存数据大小区间为[16B, 496B]数据,数组长度为32,根据数据大小计算索引的办法:数据大小除以16,如下代码所示: small   缓存数据大小区间为[512B, 4KB]数据,数组长度为4,根据数据大小计算索引的办法:数据大小除以512,然后log2得到指数,如下代码所...

Intellij IDEA 搭建Spring Boot项目(一)

Intellij IDEA 搭建Spring Boot项目 标签(空格分隔): SpringBoot JAVA后台 第一步 选择File –> New –> Project –>Spring Initialer –> 点击Next  第二步 自己修改 Group 和 Artif...

CentOS学习之路1-wget下载安装配置

参考1: https://blog.csdn.net/zhaoyanjun6/article/details/79108129 参考2: http://www.souvc.com/?p=1569 CentOS学习之路1-wget下载安装配置 1.wget的安装与基本使用 安装wget yum 安装软件 默认安装保存在/var/cache/yum ,用于所有用户使用。 帮助命令 基本用法 例子:下载...

深入浅出Spring的IOC容器,对Spring的IOC容器源码进行深入理解

文章目录 DispatcherServlet整体继承图 入口:DispatcherServlet.init() HttpServletBean.init() FrameworkServlet.initServletBean() 首先大家,去看Spring的源码入口,第一个就是DispatcherServlet DispatcherServlet整体继承图 入口:DispatcherServlet....

猜你喜欢

laravel框架的课堂知识点概总

1. MVC 1.1 概念理解 MVC全名是Model View Controller,是模型(model)-视图(view)-控制器(controller)的缩写,一种软件设计典范,用一种业务逻辑、数据、界面显示分离的方法组织代码,将业务逻辑聚集到一个部件里面,在改进和个性化定制界面及用户交互的同时,不需要重新编写业务逻辑 MVC 是一种使用 MVC(Model View Controller ...

Unity人物角色动画系统学习总结

使用动画系统控制人物行走、转向、翻墙、滑行、拾取木头 混合树用来混合多个动画 MatchTarget用来匹配翻墙贴合墙上的某一点,人物以此为支点翻墙跳跃 IK动画类似于MatchTarget,控制两只手上的两个点来指定手的旋转和位置,使得拾取木头时更逼真 创建AnimatorController: 首先创建一个混合树,然后双击 可以看到该混合树有五种状态机,分别是Idle、WalkForward、...

Composer 安装 ThinkPHP6 问题

Composer 安装 ThinkPHP6 问题 先说说问题 一.运行环境要求 二.配置 参考: ThinkPHP6.0完全开发手册 先说说问题 执行ThinkPHP6的安装命令 遇到问题汇总如下: 看提示是要更新版本,执行命令更新。 更新之后,再次安装ThinkPHP,之后遇到如下问题。 尝试了很多方法,依然不能解决。其中包括使用https://packagist.phpcomposer.com...

Spring Boot 整合JDBC

今天主要讲解一下SpringBoot如何整合JDBC,没啥理论好说的,直接上代码,看项目整体结构 看一下对应的pom.xml 定义User.java 定义数据源配置,这里使用druid,所以需要写一个配置类 上面指定druid的属性配置,和用户登录的账号信息以及对应的过滤规则: 下面定义数据访问接口和对应的实现: 数据访问层很简单,直接注入JdbcTemplate模板即可,下面再看对应的servi...

html鼠标悬停显示样式

1.显示小手:     在style中添加cursor:pointer 实现鼠标悬停变成小手样式     实例:         其他参数: cursor语法: cursor : auto | crosshair | default | hand | move | help | wait | tex...