48. 任意rot 関数定義
def rot(n, s):
※ インデント(字下げ)が同じ範囲がブロック範囲
"""rot(n, s) -> string
Return rotated n characters string of s.
"""
r = ''
for c in s:
if 'A' <= c <= 'Z':
r += chr((ord(c) - ord('A') + n) % 26 + ord('A'))
elif 'a' <= c <= 'z':
r += chr((ord(c) - ord('a') + n) % 26 + ord('a'))
else:
r += c
return r
print rot(-1, 'IBM')
49. 任意rot 内包表記版
def rot(n, s):
return ''.join(chr((ord(c) - ord('A') + n) % 26 + ord('A'))
if 'A' <= c <= 'Z'
else chr((ord(c) - ord('a') + n) % 26 + ord('a'))
if 'a' <= c <= 'z'
else c
for c in s)
あるいは
rot = lambda n, s: ''.join(
chr((ord(c) - ord('A') + n) % 26 + ord('A'))
if 'A' <= c <= 'Z'
else chr((ord(c) - ord('a') + n) % 26 + ord('a'))
if 'a' <= c <= 'z'
else c
for c in s)
56. socket通信
import socket
def solve(data):
return 'hoge'
解法処理を書く
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect(('123.45.67.89', 12345))
ホスト名とポート番号
for i in range(100):
s.send(solve(s.recv(1024)))
print s.recv(1024)
57. HTTP通信 GET
import urllib
s = urllib.urlopen('http://www.yahoo.co.jp/')
html = s.read()
s.close()
print html
58. HTTP通信 POST
import urllib, urllib2
data = urllib.urlencode({'name': 'admin',
'password': 'H4ck3r'})
s = urllib2.urlopen('http://www.../login', data)
html = s.read()
print html
59. ヘルプ
>>> help(map)
Help on built-in function map in module __builtin__:
map(...)
map(function, sequence[, sequence, ...]) -> list
Return a list of the results of applying the function to the items of
the argument sequence(s). If more than one sequence is given, the
function is called with an argument list consisting of the corresponding
item of each sequence, substituting None for missing values when not all
sequences have the same length. If the function is None, return a list of
the items of the sequence (or a list of tuples if more than one sequence).