http://www.pythonchallenge.com/pc/def/map.html php
http://wiki.pythonchallenge.com/index.php?title=Level1:Main_Page html
#!/usr/bin/env python #-*- coding: utf-8 -*- #Level1_Sol1.py import string text = "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." def Sol1(): #First, load the text and create a translation table. #string.ascii_lowercase: The lowercase letters 'abcdefghijklmnopqrstuvwxy'. This value is not locale-dependent and will not change. table = string.maketrans(string.ascii_lowercase, string.ascii_lowercase[2:]+string.ascii_lowercase[:2]) print "table is ", repr(table), "\nlen(table) is ", len(table), "\ntype(table) is ", type(table) print "------------------------" #Now, we apply the translation table on the string. newstr1 = string.translate(text, table) print "newstr1 is ", newstr1 print "------------------------" #Alternatively, just use the translate on the "text" variable. newstr2 = text.translate(table) print "text is ", text print ">>>>>>>>>>>>>>>>>>>>>>>>" print "newstr2 is ", newstr2 def Sol2(): ''' Solved without translate ''' tmp_str = "" for x in text: if ord(x) >= ord('a') and ord(x) <= ord('z'): tmp_str += chr((ord(x) + 2 - ord('a'))%26 + ord('a')) else: tmp_str += x print "tmp_str is ", tmp_str def Sol3(): ''' Similar to Sol2() but with some of 2.5's nested ternary operators ''' for x in text: print chr(ord(x) if ord(x)+2 < ord('a') else ord(x) + 2 if ord(x)+2 < ord('z') else ord(x) - 24), def Sol4(): ''' With a dictionary ''' """ zip([iterable, ...])zip()是python的一個內建函數,它接受一系列可迭代的對象做爲參數,可把兩個或多個序列中的相應項合併在一塊兒,將對象中對應的元素打包成一個個tuple元組,而後返回由這些tuples組成的list(列表)。若輸入參數的長度不等,則返回list的長度和參數中長度最短的對象相同。利用*號操做符,能夠將list unzip(解壓) >>> a = [1,2,3] >>> b = [4,5,6] >>> c = [7,8,9,0] >>> zipped = zip(a,b) >>> zipped [(1, 4), (2, 5), (3, 6)] >>> zipped = zip(a,b,c) >>> zipped [(1, 4, 7), (2, 5, 8), (3, 6, 9)] >>> zip(a,c) [(1, 7), (2, 8), (3, 9)] >>> zip(*zipped) [(1, 2, 3), (4, 5, 6), (7, 8, 9)] dict() -> new empty dictionary dict(mapping) -> new dictionary initialized from a mapping object's(key, value) pairs dict(iterable) -> new dictionary initialized as if via: d = {} for k, v in iterable: d[k] = v dict(**kwargs) -> new dictionary initialized with the name=value pairs in the keyword argument list. For example: dict(one=1, two=2) """ cypher = dict(zip(string.lowercase, string.lowercase[2:] + string.lowercase[:2])) #dict.get(key, default=None)對字典dict中的鍵key,返回它對應的值value,若是字典中不存在此鍵,則返回default的值。注意,參數default的默認值爲None. print "cypher is ", cypher print "".join(cypher.get(c,c) for c in text) if __name__ == '__main__': #Sol1() #Sol2() #Sol3() Sol4()
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." lists = str.split() list1 = [] for str1 in lists: str2 = '' for ch in str1: if ch == '.' or ch == '\'' or ch == '(' or ch == ')': str2 += ch else: str2 += chr(ord(ch)+2) str2 = str2.replace('{','a') str2 = str2.replace('|','b') list1.append(str2) print(" ".join(list1))