涉水The Python Challenge
在Stack Overflow 上看到学习Python 的一个方法是用Python 破解The Python Challenge。但我喜欢用Ruby,谁管得着呢^_^
0. 入门关很简单。
p 2**38
1. 破解一段话,观察图片很容易发现解码表把字母表循环右移两位。
riddle = "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."
riddle.scan(/./).each do |char|
if /[a-z]/ =~ char
print (?a + (char[0] - ?a + 2) % 26).chr
else
print char
end
end
译文: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.
再对url ("map")实施变换得"ocr"。
update: 发现String有个内置替换函数tr()。看,多简洁。
class String
def rot2
self.tr("a-xyz","c-zab")
end
end
p riddle.rot2
2. 查看网页源码,可以看到网页注释中有一堆乱码,上面有句话"find rare characters in the mess below:"(“找出稀少的字符”)。想到用hash 来统计各个字符的出现次数,并记录首次出现的顺序。
riddle = '...' # the mess here
count = Hash.new(0)
seq = []
riddle.scan(/./).each do |char|
seq << char if count[char] == 0
count[char] += 1
end
p count, seq
你会得到"equality"。第三关,我来了!
相关文档:
Python中执行系统命令常见方法有两种:
两者均需 import os
(1) os.system
# 仅仅在一个子终端运行系统命令,而不能获取命令执行后的返回信息
system(command) -> exit_status
Execute the command (a string) in a subshell.
# 如果再命令行下执行,结果直接打印出来
>>> os. ......
模板是简单的文本文件,它可以是html格式或是xml,csv等格式的
模板包括变量,括它会被值所替代当运行时,以及标签它控制模板的逻辑运算如if,else等
下面是一个简单的模板,我们将会对它做详细的说明
{% extends "base_generic.html" %}
{% block title %}{{ section.title }}{% endblock %}
{% block content %}
< ......
用的分别是time和datetime函数
'''
Created on 2009-9-2
@author: jiangqh
'''
import time,datetime
# date to str
print time.strftime("%Y-%m-%d %X", time.localtime())
#str to date
t = time.strptime("2009 - 08 - 08", "%Y - %m - %d")
y,m,d = t[0:3]
print datetime.datetime(y,m,d)
输出当前时间 ......
项目需要,刚刚接触python。
今天看书看到a>b==c ,a,b,c为integer
在C/C++/C#中,a>b为boolean,不可与integer比较相等
但python a>b==c等效于((a>b)&&(b==c))
在python中的写法是a>b and b==c ......