### what about making trans? ###
Hint: K->M O->Q E->G
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.
General tips:
###
根据提示,我们可以知道,该题目已经将给定的单词进行简单加密,而题目希望我们解密。
加密方式是:每个单词被前移两位,我们只需要将加密单词后移两位即可。
刚开始的时候是想直接把每个词都向后移位,但却无从下手。
突然想到以前写C程序的时候,经常会用到ASCII转换。查了下,发现了两个有用的功能函数:
ord(char):返回char的ASCII序列值
chr(num):返回num对应的ASCII字符
打开编辑器,开始活动。代码如下:
1 # !/usr/bin/env python
2 # python Challenge - map.py
3
4 str1 = " 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. "
5 for i in str1:
6 if i == " " :
7 print " " ,
8 continue
9 if i == " y " :
10 print " a " ,
11 continue
12 if i == " z " :
13 print " b " ,
14 continue
15 if i == " . " :
16 print " . " ,
17 continue
18 print chr(ord(i) + 2 ),
19 print
输出结果:
1 i h o p e y o u d i d n t t r a n s l a t e i t b y h a n d . t h a t s w h a t c o m p u t e r s a r e f o r . d o i n g i t i n b y h a n d i s i n e f f i c i e n t a n d t h a t ) s w h y t h i s t e x t i s s o l o n g . u s i n g s t r i n g . m a k e t r a n s * + i s r e c o m m e n d e d . n o w a p p l y o n t h e u r l .
2 *********************************
3
使了点小聪明,偷了懒,用了大量的if语句,非常影响了性能。并且没有检测“( )”这两个字符。知道就好~
并且查看结果可知:每个字符中间都加入了一个空格,这也是为什么我在代码中用了一个if语句来执行当遇到空格时打印两个空格。
哎,有点像火星文。
不过看了输出结果,“using string.maketrans() is recommended.”
那好吧,查查string module的makdtrans() method重写一个咯~
### another map.py ###
http://www.tutorialspoint.com/python/index.htm
这是一个不错的网址,可以查看python的模块及其方法函数,并且还有简单的代码示例。
进行string module,查到maketrans() method。
Description: This method returns a translation table that maps each character in the intab string into the character at the same position in the outtab string. Then this table is passed to the translate() function. Note that bothintab and outtab must have the same length.
Syntax: str.maketrans(intab, outtab)
Parameters:
intab: string having actual characters.
outtab: string having corresponding mapping character.
Return value: It returns a translate table to be used translate() function.
这里又涉及到另一个方法。translate() function.
Description: This method returns a copy of the string in which all characters have been translated using table(constructed with the maketrans() function in the string module), optionally deleting all characters found in the string deletechars.
Syntax: str.translate(table [, deletechars])
Parameters:
table: You can use the maketrans() helper function in the string module to create a translation table.
deletechars: list of characters to be removed from the source string.
Return value: It returns a translated copy of the string.
万事俱备,上代码:
1 # !/usr/bin/env python
2 # python challenge - maketrans.py
3
4 import string
5 str1 = " abcdefghijklmnopqrstuvwxyz "
6 str2 = " cdefghijklmnopqrstuvwxyzab "
7 transtr = string.maketrans(str1, str2)
8
9 aimstr = " 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. "
10
11 print aimstr.translate(transtr)
输出结果:
1 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.
这个结果美观多了。
最后呢,当然是进入第三题了。用地址栏里面的"map"赋值给"aimstr",重新运行即可。
得到结果"ocr"并将地址栏中的"map"替换掉,进行第三题。
done.