def calc_prod(lst):
def lazy_calc_prod():
s=1
for x in lst:
s=s*x
return s
return lazy_calc_prod
f = calc_prod([1, 2, 3, 4])
print f()
def lazy_calc_prod():
s=1
for x in lst:
s=s*x
return s
return lazy_calc_prod
f = calc_prod([1, 2, 3, 4])
print f()
2016-03-01
def cmp_ignore_case(s1, s2):
t1=s1[0].upper()
t2=s2[0].upper()
if t1>t2:
return 1
if t1<t2:
return -1
return 0
print sorted(['bob', 'about', 'Zoo', 'Credit'], cmp_ignore_case)
t1=s1[0].upper()
t2=s2[0].upper()
if t1>t2:
return 1
if t1<t2:
return -1
return 0
print sorted(['bob', 'about', 'Zoo', 'Credit'], cmp_ignore_case)
2016-03-01
import math
def is_sqr(x):
return math.sqrt(x)==int(math.sqrt(x))
print filter(is_sqr, range(1, 101))
def is_sqr(x):
return math.sqrt(x)==int(math.sqrt(x))
print filter(is_sqr, range(1, 101))
2016-03-01
def calc_prod(lst):
def lazy_calc(lst):
reduce(lambda x,y:x*y,lst,1)
return lazy_calc
f = calc_prod([1, 2, 3, 4])
print f()
def lazy_calc(lst):
reduce(lambda x,y:x*y,lst,1)
return lazy_calc
f = calc_prod([1, 2, 3, 4])
print f()
2016-03-01
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])
2016-03-01
def format_name(s):
return s[0].upper()+s[1:].lower()
print map(format_name, ['adam', 'LISA', 'barT'])
return s[0].upper()+s[1:].lower()
print map(format_name, ['adam', 'LISA', 'barT'])
2016-03-01
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-03-01
import math
def is_sqr(x):
return math.sqrt(x)==int(math.sqrt(x))
print filter(is_sqr, range(1, 101))
def is_sqr(x):
return math.sqrt(x)==int(math.sqrt(x))
print filter(is_sqr, range(1, 101))
2016-03-01
import time
def performance(f):
def fn(*args, **kw):
t1 = time.time()
r = f(*args, **kw)
t2 = time.time()
print 'call %s() in %fs' % (f.__name__, (t2 - t1))
return r
return fn
def performance(f):
def fn(*args, **kw):
t1 = time.time()
r = f(*args, **kw)
t2 = time.time()
print 'call %s() in %fs' % (f.__name__, (t2 - t1))
return r
return fn
2016-02-27