FILE_PATH = 'l1-text' f = open(FILE_PATH, 'r') text = ''.join(s.strip() for s in f.readlines()) def cmp(s): if ord(s) == 32: return ' ' elif ord(s) == 46: return '.' elif ord(s) == 39: return s elif ord(s) in [40, 41]: return s elif ord(s) > 120: return chr(ord(s)+2-26) else: return chr(ord(s)+2) print ''.join(map(cmp, text)) f.close()
我覺得我這樣寫好像很爛
compare function很弱的感覺
等我想到更好的寫法再來改
(雖然我覺得我會懶得來改)
Update:
FILE_PATH = 'l1-text' f = open(FILE_PATH, 'r') text = ''.join(s.strip() for s in f.readlines()) def cmp(s): if s > 120: return chr(s + 2 - 26) else: return chr(s + 2) print ''.join(map(cmp, [ t.isalpha() and ord(t) or ord(t) - 2 for t in text ] )) f.close()
這是強者教我的tricky method
然後這是強者寫的:
FILE_PATH = 'l1-text' print ''.join(map(lambda x: x.isalpha() and (x in 'yz' and chr(ord(x)-24) or chr(ord(x)+2)) or x,open(FILE_PATH).read())) f.close()
真是受益良多!
4 comments:
有點看得懂又有點看不懂
其實就是寫得很爛
我看出差別在哪了
那很有天份來學Python
Post a Comment