everybody thinks twice before solving this.
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.
看到这个,很直接的想到了这个方法。没有什么可以思路可以提供,这就是我看到这个题之后,最直接的想法。
就是将提供的字符串统一往后移动了2位,而且非常直观的,全是小写字母,所以直接用模运算,然后得到所有字符的ASC码值加上2之后的字符。
第一次来的字符串,有一点乱,因为没有考虑到 空格 和 ' . ' ,直接将所有字符的ASC码值加上2,最后出来的结果不太规范,处理了一下,只处理小写字母,特殊符号保持不变。代码如下:
#! usr/bin/env python
import math
import string
def translation(strin):
end = ''
for i in strin:
n = ord(i)
if n > 96 and n < 123:
n = (n-95)% 26 + 97
else:
pass
m = chr( n )
end = end + m
return str(end)
if __name__ == "__main__":
start = "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. "
print translation(start)
输出:
i hope you didnt translate it by hand. thats what aomputers are for. doing it in by hand is ineffiaient and that's why this text is so long. using string.maketrans() is reaommended. now apply on the url.
--------------Notes--------------------------------------------------------------------------------------
想要进入下一关,应该和string.maketrans()函数有一点关系,但是,不是很明白哦~然后,先学一下string.maketrans()函数。
>>> import string
>>> s = 'abcdef'
>>> table = string.maketrans('','')
>>> s.translate(table)
'abcdef'
>>> s.translate(table,'d')
'abcef'
>>> table = string.maketrans('abcde','ABCDE')
>>> s.translate(table)
'ABCDEf'
>>> table = string.maketrans('ac','12')
>>> s.translate(table)
'1b2def'
>>>
源代码:
l = map(chr, xrange(256))
_idmap = str('').join(l)
del l
_idmapL = None
def maketrans(fromstr, tostr):
"""maketrans(frm, to) -> string
Return a translation table (a string of 256 bytes long)
suitable for use in string.translate. The strings frm and to
must be of the same length.
"""
if len(fromstr) != len(tostr):
raise ValueError, "maketrans arguments must have same length"
global _idmapL
if not _idmapL:
_idmapL = list(_idmap)
L = _idmapL[:]
fromstr = map(ord, fromstr)
for i in range(len(fromstr)):
L[fromstr[i]] = tostr[i]
return ''.join(L)
xrange 和 range 的用法一样,只是range生成list,而xrange返回一个生成器。
map函数会根据提供的函数对指定序列做映射。
l = map(chr, xrange(256))
将xrange(256)中的每一个元素,调用chr,然后,返回结果的list。
所以,现在l列表里面是asc字符集
_idmap = str('').join(l)
将l里面的元素,以 ' '连接,以'string'的形式返回。
_idmap,现在是一个字符串,其元素就是256个ASC字符。
maketrans函数中,有两个参数:fromstr, tostr。
if len(fromstr) != len(tostr):
raise ValueError, "maketrans arguments must have same length"
###fromstr和tostr的长度应该一样。
if not _idmapL:
_idmapL = list(_idmap)
L = _idmapL[:]
#### L 变成ASC字符集
fromstr = map(ord, fromstr)
####fromstr变成每一个ASC字符对应的ASC码值,之前fromstr[0]='a',现在fromstr[0] = '97'
for i in range(len(fromstr)):
L[fromstr[i]] = tostr[i]
####将L中的字符变成,相应的想要变换的字符。fromstr = 'a' tostr = '1',在L中,本来应该是'a'的地方就变成了'1'。
最后,返回一个list。从函数的__doc__中可以看到,maketrans函数是为了translate方法做准备的。
# Character translation through look-up table.
def translate(s, table, deletions=""):
"""translate(s,table [,deletions]) -> string
Return a copy of the string s, where all characters occurring
in the optional argument deletions are removed, and the
remaining characters have been mapped through the given
translation table, which must be a string of length 256. The
deletions argument is not allowed for Unicode strings.
"""
if deletions or table is None:
return s.translate(table, deletions)
else:
# Add s[:0] so that if s is Unicode and table is an 8-bit string,
# table is converted to Unicode. This means that table *cannot*
# be a dictionary -- for that feature, use u.translate() directly.
return s.translate(table + s[:0])
恩,又有一点不明白了~怎么破~
。。。。。。。。。
map -> ocr
第二关:
http://www.pythonchallenge.com/pc/def/ocr.html