言語処理100本ノックやった 07-08 #python
#07 テンプレートによる文生成
引数x, y, z を受け取り「x時のyはz」を返す関数
x = input() y = raw_input() z = raw_input() print '「%d時の%sは%s」' %(x, y, z)
実行結果
「関数を実装せよ」って書いてるから def ~ : にしなきゃだめか。まあいいか。
#08 暗号文
- 英小文字ならば(219-文字コード)に置換
- その他の文字はそのまま
とする関数
def cipher(inp): ans = '' for s in inp: if s.islower(): code = ord(s) # e.g. ord('a') returns the integer 97 code = 219 - code ans += chr(code) # Return a string of one character whose ASCII code is the integer i. else: ans += s return ans print cipher('No one can live without water') # Nl lmv xzm orev drgslfg dzgvi print cipher('Nl lmv xzm orev drgslfg dzgvi') # No one can live without water print cipher('abcdefghijklmnopqrstuvwxyz') # zyxwvutsrqponmlkjihgfedcba
ord() で文字コードを得て、chr() で文字に戻す
2. Built-in Functions — Python v2.6.9 documentation
今日はこのへんで