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
最赞回答 / no_nicheng
math.sqrt的返回值是浮点数,所以你的"if isinstance(math.sqrt(x),int)"必然是False的,所以is_sqr()一定返回None,转换为布尔型,即为Falsefilter()的第一个参数,要求为布尔型,可你调用is_sqr()都会返回False,那么必然一个数值都筛选不出来,自然返回的数据为空
2016-02-22
已采纳回答 / modric
你了解过冒泡排序吗?在冒泡排序中一个关键问题就是两个元素比较大小来决定谁在前,谁在后比如1,3,8,5 排序出来应该是1,3,5,8 这种自然数字比较直观, 隐含的比如 8 , 5的位置就是: if 8 < 5: return -1 if 8 > 5: return 1 return 0这个函数就是提供一个两个元素比较的方法,如果return 0 说明两个元素不分先后,可以随意
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
更相减损术
def __str__(self):
s = self.p
t = self.q
while s != t:
if s < t:
t = t - s
else:
s = s - t
return '%s/%s' %(self.p/s, self.q/t)
def __str__(self):
s = self.p
t = self.q
while s != t:
if s < t:
t = t - s
else:
s = s - t
return '%s/%s' %(self.p/s, self.q/t)
2016-02-21
def calc_prod(lst):
def lazy_prod():
def f(x,y):
return x * y
return reduce(f, lst, 1)
return lazy_prod
f = calc_prod([1, 2, 3, 4])
print f()
def lazy_prod():
def f(x,y):
return x * y
return reduce(f, lst, 1)
return lazy_prod
f = calc_prod([1, 2, 3, 4])
print f()
2016-02-21