# 希望一次返回3个函数,分别计算1x1,2x2,3x3:
def count():
fs = []
for i in range(1, 4):
def f(j):
def g():
return j*j
return g
r = f(i)
fs.append(r)
return fs
f1, f2, f3 = count()
print(f1(), f2(), f3())
def count():
fs = []
for i in range(1, 4):
def f(j):
def g():
return j*j
return g
r = f(i)
fs.append(r)
return fs
f1, f2, f3 = count()
print(f1(), f2(), f3())
2018-03-12
class Person(object):
count = 0
def __init__(self, name):
self.name = name
Person.count = Person.count +1
p1 = Person('Bob')
print Person.count
p2 = Person('Alice')
print Person.count
p3 = Person('Tim')
print Person.count
count = 0
def __init__(self, name):
self.name = name
Person.count = Person.count +1
p1 = Person('Bob')
print Person.count
p2 = Person('Alice')
print Person.count
p3 = Person('Tim')
print Person.count
2018-03-12
import math
def is_sqr(x):
if math.sqrt(x)-int (math.sqrt(x))==0:
return x
print filter(is_sqr, range(1, 101))
def is_sqr(x):
if math.sqrt(x)-int (math.sqrt(x))==0:
return x
print filter(is_sqr, range(1, 101))
2018-03-12
gcd(b, a%b)这tm是欧几里得算法。即两个数的最大公约数等于其中较小的那个数和两数相除余数的最大公约数。
吓得我虎躯一震
吓得我虎躯一震
2018-03-11
def __str__(self):
for x in [2, 3, 5, 7]:
while self.p % x == 0 and self.q % x == 0:
self.p = self.p / x
self.q = self.q / x
return '%s/%s' % (self.p, self.q)
for x in [2, 3, 5, 7]:
while self.p % x == 0 and self.q % x == 0:
self.p = self.p / x
self.q = self.q / x
return '%s/%s' % (self.p, self.q)
2018-03-11
def __init__(self, num):
self.sum = []
for i in range(num):
if i <= 1:
self.sum.append(i)
else:
self.sum.append(self.sum[i-1] + self.sum[i-2])
def __str__(self):
return str(self.sum)
self.sum = []
for i in range(num):
if i <= 1:
self.sum.append(i)
else:
self.sum.append(self.sum[i-1] + self.sum[i-2])
def __str__(self):
return str(self.sum)
2018-03-10
import math
def is_sqr(x):
return int(math.sqrt(x))*int(math.sqrt(x))==x
print filter(is_sqr, range(1, 101))
def is_sqr(x):
return int(math.sqrt(x))*int(math.sqrt(x))==x
print filter(is_sqr, range(1, 101))
2018-03-10
class Fib(object):
def __call__(self,num):
list = [0,1]
for x in range(0,num-2):
list.append(list[-2]+list[-1])
return list
f = Fib()
print f(10)
def __call__(self,num):
list = [0,1]
for x in range(0,num-2):
list.append(list[-2]+list[-1])
return list
f = Fib()
print f(10)
2018-03-10