[Level 1]

Title: What about making trans?
圖片下方有兩段話,但只有第一段能識別:
everybody thinks twice before solving this.
而圖中K→M,O→Q,E→G,理清其中映射關系,思路就有了。
import string
table = str.maketrans(string.ascii_lowercase,string.ascii_lowercase[2:] + 'ab')
s.translate(table)#s是要轉(zhuǎn)換的字符串
轉(zhuǎn)換第二段話得到:
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.
轉(zhuǎn)換url中的map,得到ocr,[Level 2]
小結
使用轉(zhuǎn)換表轉(zhuǎn)換每一個字符。
-
string.ascii_lowercase表示26個順序小寫字母 -
static str.maketrans(x[, y[, z]]),方法返回用于str.translate()的轉(zhuǎn)換表。
只一個參數(shù)時,類型必須是dict,鍵為Unicode ordinals(整數(shù))或字符,值為任意長度字符串或者None,鍵對應的字符將被轉(zhuǎn)換為值對應的字符(串);如果有兩個參數(shù),參數(shù)長度必須相等且x中每一個字符對應轉(zhuǎn)換y中每一個字符;如果有第三個參數(shù),對應的字符轉(zhuǎn)換為None。 -
str.translate(table)返回通過給定的翻譯表映射每個字符的字符串的副本。
Python Challenge Wiki
1. 使用chr()和ord()列表解析
"".join(map(lambda x: x.isalpha() and chr((ord(x)+2-ord("a"))%26 + ord("a")) or x, input))"".join([(c.isalpha() and chr(ord('a') + (ord(c)-ord('a')+2)%26) or c) for c in "map"])
思路小解
相同的思路,在Ascii碼和字符之間進行轉(zhuǎn)換得到結果。
-
str.isalpha()函數(shù)判斷字符串是否是字母。 -
chr(i)返回表示Unicode碼點為整數(shù)i的字符的字符串;ord(c)返回一個表示該字符的Unicode代碼點的整數(shù)。 -
map(function, iterable, ...),應用function方法,(并行)返回參數(shù)中的迭代元素。 -
lambda表達式相當于創(chuàng)建一個匿名函數(shù)。 -
str.join(iterable),返回迭代器里的所有字符串。