import functools
sorted_ignore_case = functools.partial(sorted,key = lambda a:a.lower())
print sorted_ignore_case(['bob', 'about', 'Zoo', 'Credit'])
sorted_ignore_case = functools.partial(sorted,key = lambda a:a.lower())
print sorted_ignore_case(['bob', 'about', 'Zoo', 'Credit'])
2018-12-29
import functools
sorted_ignore_case = functools.partial(sorted,cmp=lambda a,b:cmp(a.lower(),b.lower()))
print sorted_ignore_case(['bob', 'about', 'Zoo', 'Credit'])
sorted_ignore_case = functools.partial(sorted,cmp=lambda a,b:cmp(a.lower(),b.lower()))
print sorted_ignore_case(['bob', 'about', 'Zoo', 'Credit'])
2018-12-29
def per(unit):
def fw(f):
@functools.wraps(f)
def fun(*args, **kv):
t1 = time.time()
res = f(*args, **kv)
print('call %s() %f %s' %
(f.__name__, (time.time()-t1), unit))
return res
return fun
return fw
def fw(f):
@functools.wraps(f)
def fun(*args, **kv):
t1 = time.time()
res = f(*args, **kv)
print('call %s() %f %s' %
(f.__name__, (time.time()-t1), unit))
return res
return fun
return fw
2018-12-29
def per(unit):
def fwrapper(f):
def fun(*args,**kv):
t1 = time.time()
res = f(*args,**kv)
print 'call %s() used %f %s'%(f.__name__,(time.time()-t1)*1000,unit)
return res
return fun
return fwrapper
@per('ms')
def factorial(n):
def fwrapper(f):
def fun(*args,**kv):
t1 = time.time()
res = f(*args,**kv)
print 'call %s() used %f %s'%(f.__name__,(time.time()-t1)*1000,unit)
return res
return fun
return fwrapper
@per('ms')
def factorial(n):
2018-12-29
import time
def per(f):
def fn(*args, **kw):
t1 = time.time()
res = f(*args,**kw)
print 'call %s() in %f ms'%(f.__name__,time.time()-t1)
return res
return fn
@per
def factorial(n):
return reduce(lambda x,y: x*y, range(1, n+1))
print factorial(10)
def per(f):
def fn(*args, **kw):
t1 = time.time()
res = f(*args,**kw)
print 'call %s() in %f ms'%(f.__name__,time.time()-t1)
return res
return fn
@per
def factorial(n):
return reduce(lambda x,y: x*y, range(1, n+1))
print factorial(10)
2018-12-29
import time
def perf(f):
def fn(*args, **kw):
t1 = time.time()
res = f(*args,**kw)
print 'call factorial() in %f ms'%(time.time()-t1)
return res
return fn
@perf
def factorial(n):
return reduce(lambda x,y: x*y, range(1, n+1))
print factorial(10)
def perf(f):
def fn(*args, **kw):
t1 = time.time()
res = f(*args,**kw)
print 'call factorial() in %f ms'%(time.time()-t1)
return res
return fn
@perf
def factorial(n):
return reduce(lambda x,y: x*y, range(1, n+1))
print factorial(10)
2018-12-29
print filter(lambda s:s and len(s.strip())>0, ['test', None, '', 'str', ' ', 'END'])
2018-12-29
def count():
fs = []
for i in range(1, 4):
def f(j=i):
return j*j
fs.append(f)
return fs
f1, f2, f3 = count()
print f1(), f2(), f3()
fs = []
for i in range(1, 4):
def f(j=i):
return j*j
fs.append(f)
return fs
f1, f2, f3 = count()
print f1(), f2(), f3()
2018-12-29
from functools import reduce
def calc_prod(lst):
def cal():
return reduce(lambda x, y: x*y, lst)
return cal
f = calc_prod([1, 2, 3, 4])
print(f())
def calc_prod(lst):
def cal():
return reduce(lambda x, y: x*y, lst)
return cal
f = calc_prod([1, 2, 3, 4])
print(f())
2018-12-29
def cmp_ignore_case(s1, s2):
return cmp(s1.lower(),s2.lower())
print sorted(['bob', 'about', 'Zoo', 'Credit'], cmp_ignore_case)
return cmp(s1.lower(),s2.lower())
print sorted(['bob', 'about', 'Zoo', 'Credit'], cmp_ignore_case)
2018-12-29
import math
def is_sqr(x):
return math.sqrt(x)%1==0
print filter(is_sqr, range(1, 101))
def is_sqr(x):
return math.sqrt(x)%1==0
print filter(is_sqr, range(1, 101))
2018-12-29
def prod(x, y):
return x*y
print reduce(prod, [2, 4, 5, 7, 12])
return x*y
print reduce(prod, [2, 4, 5, 7, 12])
2018-12-29
def format_name(s):
return s[:1].upper()+s[1:].lower()
print map(format_name, ['adam', 'LISA', 'barT'])
return s[:1].upper()+s[1:].lower()
print map(format_name, ['adam', 'LISA', 'barT'])
2018-12-29
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)
2018-12-29
def prod(x, y):
return x * y
print(reduce(prod,[2, 4, 5, 7, 12]))
return x * y
print(reduce(prod,[2, 4, 5, 7, 12]))
2018-12-27