#标准答案
def calc_prod(lst):
def lazy_prod():
def f(x,y):
return x*y
return reduce(f,lst,1)
return lazy_prod
f = calc_prod([1, 2, 3, 4])
print f()
def calc_prod(lst):
def lazy_prod():
def f(x,y):
return x*y
return reduce(f,lst,1)
return lazy_prod
f = calc_prod([1, 2, 3, 4])
print f()
2018-01-05
def cmp_ignore_case(s1, s2):
if s1[:1].lower() < s2[:1].lower(): #首字母变小写,使用倒序的逻辑也可
return -1
if s1[:1].lower() < s2[:1].lower():
return 1
return 0
print sorted(['bob', 'about', 'Zoo', 'Credit'], cmp_ignore_case)
if s1[:1].lower() < s2[:1].lower(): #首字母变小写,使用倒序的逻辑也可
return -1
if s1[:1].lower() < s2[:1].lower():
return 1
return 0
print sorted(['bob', 'about', 'Zoo', 'Credit'], cmp_ignore_case)
2018-01-05
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-01-05
def format_name(s):
return s.title()
print map(format_name, ['adam', 'LISA', 'barT'])
return s.title()
print map(format_name, ['adam', 'LISA', 'barT'])
2018-01-04
def __init__(self, num):
self.num = num
self.d = self.x()
def __len__(self):
return len(self.d)
self.num = num
self.d = self.x()
def __len__(self):
return len(self.d)
2018-01-04
def a(L):
for s in L:
if isinstance(s,Student):
pass
else:
return 'Bug'
b = a(L)
if b == 'Bug':
print b
else:
print sorted(L)
for s in L:
if isinstance(s,Student):
pass
else:
return 'Bug'
b = a(L)
if b == 'Bug':
print b
else:
print sorted(L)
2018-01-04
print '<Student: name, gender, score>'
2018-01-04
def __init__(self, name, gender, **kw):
self.name = name
self.gender = gender
setattr(self,'dd',kw)
for k,v in getattr(self,'dd').items():
setattr(self,k,v)
self.name = name
self.gender = gender
setattr(self,'dd',kw)
for k,v in getattr(self,'dd').items():
setattr(self,k,v)
2018-01-04
class Person(object):
__count = 0
def __init__(self, name):
self.name = name
Person.__count = Person.__count +1
def put_count(self):
print Person.__count
__count = 0
def __init__(self, name):
self.name = name
Person.__count = Person.__count +1
def put_count(self):
print Person.__count
2018-01-04
p1 = Person('Bob')
p1.put_count()
p2 = Person('Alice')
p2.put_count()
try :
print Person.__count
except AttributeError:
print 'attributeerror'
p1.put_count()
p2 = Person('Alice')
p2.put_count()
try :
print Person.__count
except AttributeError:
print 'attributeerror'
2018-01-04
import math
def is_sqr(x):
y=math.sqrt(x)
if y.is_integer():
return y
print filter(is_sqr, range(1,101))
def is_sqr(x):
y=math.sqrt(x)
if y.is_integer():
return y
print filter(is_sqr, range(1,101))
2018-01-04