def calc_prod(lst):
def prod():
x = 1
for y in lst:
x = x * y
return x
return prod
f = calc_prod([1, 2, 3, 4])
print f()
def prod():
x = 1
for y in lst:
x = x * y
return x
return prod
f = calc_prod([1, 2, 3, 4])
print f()
2016-05-04
def calc_prod(lst):
def lazy():
def xx(x,y):
return x*y
return reduce(xx,lst)
return lazy
f = calc_prod([1, 2, 3, 4])
print f()
def lazy():
def xx(x,y):
return x*y
return reduce(xx,lst)
return lazy
f = calc_prod([1, 2, 3, 4])
print f()
2016-05-03
def cmp_ignore_case(s1, s2):
a=s1.lower()
b=s2.lower()
if a > b:
return 1
if a < b:
return -1
print sorted(['bob', 'about', 'Zoo', 'Credit'], cmp_ignore_case)
a=s1.lower()
b=s2.lower()
if a > b:
return 1
if a < b:
return -1
print sorted(['bob', 'about', 'Zoo', 'Credit'], cmp_ignore_case)
2016-05-03
import math
def is_sqr(x):
return math.sqrt(x)*10%10 == 0
print (list(filter(is_sqr, range(1, 101))))
def is_sqr(x):
return math.sqrt(x)*10%10 == 0
print (list(filter(is_sqr, range(1, 101))))
2016-05-03
def prod(x, y):
return x*y
print reduce(prod, [2, 4, 5, 7, 12])
python 内置函数果然好强大
return x*y
print reduce(prod, [2, 4, 5, 7, 12])
python 内置函数果然好强大
2016-05-03
def performance(unit):
def time_per(f):
def wrapper(n):
t1 = time.time()
result = f(n)
t2 = time.time()
print 'call ' + f.__name__ + '() in ' + str(t2 - t1) + unit
return result
return wrapper
return time_per
def time_per(f):
def wrapper(n):
t1 = time.time()
result = f(n)
t2 = time.time()
print 'call ' + f.__name__ + '() in ' + str(t2 - t1) + unit
return result
return wrapper
return time_per
2016-05-02
import time
def performance(f):
def fn(*args, **kw):
t1 = time.time()
result = f(*args, **kw)
t2 = time.time()
print 'call ' + f.__name__ + '() in ' + str(t2 - t1) + 's'
return result
return fn
def performance(f):
def fn(*args, **kw):
t1 = time.time()
result = f(*args, **kw)
t2 = time.time()
print 'call ' + f.__name__ + '() in ' + str(t2 - t1) + 's'
return result
return fn
2016-05-02