[Python Challenge] - Q1

Q1:What about making trans?everybody thinks twice before solving this.

 

http://www.pythonchallenge.com/pc/def/map.html

 

题目的意思是将给定的字符串按照图中"K-M O-Q E-G"的方式替代,于是有了下面的方法:

 

代码1:

str="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. " for s in str: if ord(s)==121 or ord(s)==122: s=chr(ord(s)-24) elif 96

 

输出结果:

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 .  

 

看输出结果,作者建议了string.maketrans()方法,查了一下,又有了下面的代码:

 

代码2:

import string str = "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. " intab = "abcdefghijklmnopqrstuvwxyz" outtab = "cdefghijklmnopqrstuvwxyzab" transtab = string.maketrans(intab,outtab) str.translate(transtab)

 

输出结果:

"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按照规则替换为ocr即可。

 

另学习一下string.maketrans()方法(我理解的):

此方法返回一个table,用以指示intab和outtab两个字符串对应的替代位置,注意intab和outtab应具有相同长度。

你可能感兴趣的:(Python)