[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:

 

輸出結果:

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:

 

輸出結果:

"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應具有相同長度。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章