j = 0
def count():
fs = []
for i in range(1,4):
def f():
global j
j += 1
return j*j
fs.append(f)
return fs
f1, f2, f3 = count()
print f1(), f2(), f3()
def count():
fs = []
for i in range(1,4):
def f():
global j
j += 1
return j*j
fs.append(f)
return fs
f1, f2, f3 = count()
print f1(), f2(), f3()
2018-10-04
def calc_prod(lst):
def prod():
return reduce(lambda x,y: x*y,lst)
return prod
f = calc_prod([1, 2, 3, 4])
print f()
def prod():
return reduce(lambda x,y: x*y,lst)
return prod
f = calc_prod([1, 2, 3, 4])
print f()
2018-10-04
def cmp_ignore_case(s1, s2):
if s1[0].upper() > s2[0].upper():
return 1
if s1[0].upper() < s2[0].upper():
return -1
return 0
print sorted(['bob', 'about', 'Zoo', 'Credit'], cmp_ignore_case)
if s1[0].upper() > s2[0].upper():
return 1
if s1[0].upper() < s2[0].upper():
return -1
return 0
print sorted(['bob', 'about', 'Zoo', 'Credit'], cmp_ignore_case)
2018-10-04
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-10-04
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'])
2018-10-04
def __str__(self):
#p,q=float.as_integer_ratio(self.p/self.q)
#return '{}/{}'.format(p,q)
from fractions import Fraction
#字面意思,numerator获取分子,denominator获取分母
r=Fraction(self.p,self.q)
return '{}/{}'.format(r.numerator,r.denominator)
#p,q=float.as_integer_ratio(self.p/self.q)
#return '{}/{}'.format(p,q)
from fractions import Fraction
#字面意思,numerator获取分子,denominator获取分母
r=Fraction(self.p,self.q)
return '{}/{}'.format(r.numerator,r.denominator)
2018-09-29
def __str__(self):
p,q=float.as_integer_ratio(self.p/self.q)
#float.as_integer_ratio(6/8) #Out[24]: (3, 4)
return '{}/{}'.format(p,q)
p,q=float.as_integer_ratio(self.p/self.q)
#float.as_integer_ratio(6/8) #Out[24]: (3, 4)
return '{}/{}'.format(p,q)
2018-09-29
def __str__(self):
s=[]
a,b=0,1
for i in range(self.num):
a,b=a+b,a
s.append(b)
return str(s)
s=[]
a,b=0,1
for i in range(self.num):
a,b=a+b,a
s.append(b)
return str(s)
2018-09-29
def __cmp__(self, s):
if self.score==s.score:
return -1 if self.name<s.name else 1
return 1 if self.score<s.score else -1
if self.score==s.score:
return -1 if self.name<s.name else 1
return 1 if self.score<s.score else -1
2018-09-29
def calc_prod(lst):
def lazy_prod():
return reduce(lambda x,y:x*y,lst)
return lazy_prod
f = calc_prod([1, 2, 3, 4])
print f()
def lazy_prod():
return reduce(lambda x,y:x*y,lst)
return lazy_prod
f = calc_prod([1, 2, 3, 4])
print f()
2018-09-29