def format_name(s):
return str.capitalize(s)
print map(format_name, ['adam', 'LISA', 'barT'])
return str.capitalize(s)
print map(format_name, ['adam', 'LISA', 'barT'])
2016-02-23
import math
def add(x, y, f):
return f(x) + f(y)
print add(25, 9, math.sqrt)
def add(x, y, f):
return f(x) + f(y)
print add(25, 9, math.sqrt)
2016-02-23
t=[x for x in xrange(1,101)]
print reduce(prod, t)
另外一种方式。
print reduce(prod, t)
另外一种方式。
2016-02-23
s=[]
a=[1, 2, 3, 4, 5, 6, 7, 8, 9]
for a in a:
x=a*a
s.append(x)
print s
另外一种做法
a=[1, 2, 3, 4, 5, 6, 7, 8, 9]
for a in a:
x=a*a
s.append(x)
print s
另外一种做法
2016-02-23
def abe(x):
return '-x'
def add(x, y, abe):
return abe(x) + abe(y)
def abe(x):
return (-x)
print add(25, 9, abe)
return '-x'
def add(x, y, abe):
return abe(x) + abe(y)
def abe(x):
return (-x)
print add(25, 9, abe)
2016-02-23
import time
def performance(unit):
def time_f(f):
def mod_f(*args, **kw):
t1=time.time()
r=f(*args, **kw)
t2=time.time()
print 'call %s() in %f%s' %(f.__name__, (t2-t1),unit)
return r
return mod_f
return time_f
剩下一樣
def performance(unit):
def time_f(f):
def mod_f(*args, **kw):
t1=time.time()
r=f(*args, **kw)
t2=time.time()
print 'call %s() in %f%s' %(f.__name__, (t2-t1),unit)
return r
return mod_f
return time_f
剩下一樣
2016-02-23
def calc_prod(lst):
def lazy_prod():
a = 1
for b in lst:
a = a * b
return a
return lazy_prod
f = calc_prod([1, 2, 3, 4])
print f()
def lazy_prod():
a = 1
for b in lst:
a = a * b
return a
return lazy_prod
f = calc_prod([1, 2, 3, 4])
print f()
2016-02-23
import time
def performance(f):
def mod_f(*args, **kw):
t1=time.time()
r=f(*args, **kw)
t2=time.time()
print 'call %s() in %fs' %(f.__name__, (t2-t1))
return r
return mod_f
@performance
剩下一樣...
def performance(f):
def mod_f(*args, **kw):
t1=time.time()
r=f(*args, **kw)
t2=time.time()
print 'call %s() in %fs' %(f.__name__, (t2-t1))
return r
return mod_f
@performance
剩下一樣...
2016-02-23
def prod(x,y):
return x*y
def calc_prod(lst):
def lazy_prod():
return reduce(prod,lst)
return lazy_prod
f = calc_prod([1, 2, 3, 4])
print f()
return x*y
def calc_prod(lst):
def lazy_prod():
return reduce(prod,lst)
return lazy_prod
f = calc_prod([1, 2, 3, 4])
print f()
2016-02-23
import time
def performance(f):
def fn(*args, **kw):
print 'call ' + f.__name__ + '() in',time.ctime()
return f(*args, **kw)
return fn
@performance
def factorial(n):
return reduce(lambda x,y: x*y, range(1, n+1))
print factorial(10)
def performance(f):
def fn(*args, **kw):
print 'call ' + f.__name__ + '() in',time.ctime()
return f(*args, **kw)
return fn
@performance
def factorial(n):
return reduce(lambda x,y: x*y, range(1, n+1))
print factorial(10)
2016-02-22
import time
def performance(unit):
def log(f):
def time_start(*args):
t = time.strftime('%Y-%m-%d %H:%M:%S:%MS')
print 'call' , f.__name__ + '()in' , t , 'ms'
return f(*args)
return time_start
return log
def performance(unit):
def log(f):
def time_start(*args):
t = time.strftime('%Y-%m-%d %H:%M:%S:%MS')
print 'call' , f.__name__ + '()in' , t , 'ms'
return f(*args)
return time_start
return log
2016-02-22
辗转相除法
def __str__(self):
s = self.p
t = self.q
while t != 0:
s, t = t, s%t
return '%s/%s' %(self.p/s, self.q/s)
def __str__(self):
s = self.p
t = self.q
while t != 0:
s, t = t, s%t
return '%s/%s' %(self.p/s, self.q/s)
2016-02-21