def calc_prod(lst):
def bf_mul(x,y):
return x*y
def mul():
return reduce(bf_mul,lst)
return mul
f = calc_prod([1, 2, 3, 4])
print f()
def bf_mul(x,y):
return x*y
def mul():
return reduce(bf_mul,lst)
return mul
f = calc_prod([1, 2, 3, 4])
print f()
2015-06-09
import math
def is_sqr(x):
return math.sqrt(x)%round(math.sqrt(x))==0
print filter(is_sqr, range(1, 101))
#是不是略繁琐
def is_sqr(x):
return math.sqrt(x)%round(math.sqrt(x))==0
print filter(is_sqr, range(1, 101))
#是不是略繁琐
2015-06-08
import math
def is_sqr(x):
return math.sqrt(x)%1==0
print filter(is_sqr, range(1, 101))
判断是否为整数可以让数对1取余数,如果为0就是整数
def is_sqr(x):
return math.sqrt(x)%1==0
print filter(is_sqr, range(1, 101))
判断是否为整数可以让数对1取余数,如果为0就是整数
2015-06-08
def performance(f):
def fn(*args, **kw):
print 'call ' + f.__name__ + '()...' + 'time is:' + time.strftime('%Y-%m-%d %H:%M',time.localtime(time.time()))
return f(*args, **kw)
return fn
为什么要算时间差,我这样写是对的吗,输出的时间是调用函数的时间吗?
def fn(*args, **kw):
print 'call ' + f.__name__ + '()...' + 'time is:' + time.strftime('%Y-%m-%d %H:%M',time.localtime(time.time()))
return f(*args, **kw)
return fn
为什么要算时间差,我这样写是对的吗,输出的时间是调用函数的时间吗?
2015-06-08
print isinstance(t, object)
print isinstance(t, Person)
print isinstance(t, Teacher)
print isinstance(t, Student)
答案判定条件太死板了,一定要按照tftt的顺序,tttf多整洁啊
print isinstance(t, Person)
print isinstance(t, Teacher)
print isinstance(t, Student)
答案判定条件太死板了,一定要按照tftt的顺序,tttf多整洁啊
2015-06-07
n=max(self.p,self.q)
for i in range(1,n)[::-1]:
if (self.p%i==0)and (self.q%i==0):
self.q=self.q/i
self.p=self.p/i
return '%s/%s' % (self.p,self.q)
__repr__ = __str__
for i in range(1,n)[::-1]:
if (self.p%i==0)and (self.q%i==0):
self.q=self.q/i
self.p=self.p/i
return '%s/%s' % (self.p,self.q)
__repr__ = __str__
2015-06-07
def __init__(self, num):
self.num=num
xfid=[0,1]
for n in range(1,num-1):
xfid.append(xfid[-1]+xfid[-2])
self.lst=xfid
def __str__(self):
return '%s' % self.lst
def __len__(self):
return len(self.lst)
self.num=num
xfid=[0,1]
for n in range(1,num-1):
xfid.append(xfid[-1]+xfid[-2])
self.lst=xfid
def __str__(self):
return '%s' % self.lst
def __len__(self):
return len(self.lst)
2015-06-07
class Fib(object):
def __call__(self, num):
a, b, L = 0, 1, []
for n in range(num):
L.append(a)
a, b = b, a + b
return L
f = Fib()
print f(10)
def __call__(self, num):
a, b, L = 0, 1, []
for n in range(num):
L.append(a)
a, b = b, a + b
return L
f = Fib()
print f(10)
2015-06-04
def __call__(self, num):
self.fib = []
if num == 0:
self.fib.append(0)
else:
self.fib.append(0)
self.fib.append(1)
for x in range(num-2):
self.fib.append(self.fib[x] + self.fib[x+1])
return ' '.join([str(self.fib[i]) for i in range(num)])
self.fib = []
if num == 0:
self.fib.append(0)
else:
self.fib.append(0)
self.fib.append(1)
for x in range(num-2):
self.fib.append(self.fib[x] + self.fib[x+1])
return ' '.join([str(self.fib[i]) for i in range(num)])
2015-06-04