def cmp_ignore_case(s1, s2):
if s1.lower()>s2.lower():
return 1
if s1.lower()<s2.lower():
return -1
return 0
print sorted(['bob', 'about', 'Zoo', 'Credit'], cmp_ignore_case)
if s1.lower()>s2.lower():
return 1
if s1.lower()<s2.lower():
return -1
return 0
print sorted(['bob', 'about', 'Zoo', 'Credit'], cmp_ignore_case)
2016-08-14
import math
def is_sqr(x):
a=int(math.sqrt(x))
return a*a==x
print filter(is_sqr, range(1,101 ))
def is_sqr(x):
a=int(math.sqrt(x))
return a*a==x
print filter(is_sqr, range(1,101 ))
2016-08-14
由于读不懂廖老师的目的,所以我也不知道自己的代码到底是凑巧输出对了,还是真的符合题意
import json
class Students(object):
def __init__(self,s) :
self.s=s
def read(self) :
return self.s
s = Students(r'["Tim", "Bob", "Alice"]')
print json.load(s)
import json
class Students(object):
def __init__(self,s) :
self.s=s
def read(self) :
return self.s
s = Students(r'["Tim", "Bob", "Alice"]')
print json.load(s)
2016-08-14
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-08-13
import json
class Students(object):
def __init__(self,name):
self.name=name
def read(self):
return self.name.encode('utf-8')
s = Students(u'["Tim", "Bob", "Alice"]')
print json.load(s)
提示要输出unicode的字符
class Students(object):
def __init__(self,name):
self.name=name
def read(self):
return self.name.encode('utf-8')
s = Students(u'["Tim", "Bob", "Alice"]')
print json.load(s)
提示要输出unicode的字符
2016-08-13
print "call %s() in 3628800" % (f.__name__ )
哎,谁规定的程序运行时间还必须一样,题目参数还不给完,这不是逼着看答案吗
哎,谁规定的程序运行时间还必须一样,题目参数还不给完,这不是逼着看答案吗
2016-08-12
fs.append(f)说的是向数组内添加函数,是添加 i*i ,所以只要在for循环内把i*i计算出来添加到数组内就OK了,我的代码:
def count():
fs = []
for i in range(1, 4):
def f():
return i*i
a = f()
fs.append(a)
return fs
f1, f2, f3 = count()
print f1,f2,f3
def count():
fs = []
for i in range(1, 4):
def f():
return i*i
a = f()
fs.append(a)
return fs
f1, f2, f3 = count()
print f1,f2,f3
2016-08-12
import time
def performance(f):
def fn(*arg,**kw):
print 'call'+f.__name__+'() in'+time.asctime()
return f(*arg,**kw)
return fn
@performance
def factorial(n):
return reduce(lambda x,y: x*y, range(1, n+1))
print factorial(10)
答案竟然会匹配是否有call factorial() in
def performance(f):
def fn(*arg,**kw):
print 'call'+f.__name__+'() in'+time.asctime()
return f(*arg,**kw)
return fn
@performance
def factorial(n):
return reduce(lambda x,y: x*y, range(1, n+1))
print factorial(10)
答案竟然会匹配是否有call factorial() in
2016-08-12
我看楼下有的人说把fs.append(f)改为fs.append(f()),我觉得不对,这样count返回的列表fs中的元素是int类型而不是函数,使用print f1可以输出内容,但是使用print f1()时是会报错的。
2016-08-12