我的算是正解之一:
import math
def is_sqr(x):
return x and math.modf(math.sqrt(x))[0] == 0
print filter(is_sqr, range(1, 101))
import math
def is_sqr(x):
return x and math.modf(math.sqrt(x))[0] == 0
print filter(is_sqr, range(1, 101))
2017-12-21
class Fib(object):
def __init__(self):
pass
def fibonaci(self,n):
a, b, L = 0, 1, []
for n in range(n):
L.append(a)
a, b = b, a + b
return L
def __call__(self,m):
return self.fibonaci(m)
f = Fib()
print f(10)
def __init__(self):
pass
def fibonaci(self,n):
a, b, L = 0, 1, []
for n in range(n):
L.append(a)
a, b = b, a + b
return L
def __call__(self,m):
return self.fibonaci(m)
f = Fib()
print f(10)
2017-12-20
def cmp_ignore_case(s1, s2):
for i in range(0,10):
if s1[i].lower() > s2[i].lower():
return 1
break
elif s1[i].lower() < s2[i].lower():
return -1
break
return 0
print sorted(['bob', 'about', 'Zoo', 'Credit'], cmp_ignore_case)
改
for i in range(0,10):
if s1[i].lower() > s2[i].lower():
return 1
break
elif s1[i].lower() < s2[i].lower():
return -1
break
return 0
print sorted(['bob', 'about', 'Zoo', 'Credit'], cmp_ignore_case)
改
2017-12-20
def cmp_ignore_case(s1, s2):
for i in range(0,10):
if s1[i].lower() > s2.lower():
return 1
break
elif s1[i].lower() < s2.lower():
return -1
break
return 0
print sorted(['bob', 'about', 'Zoo', 'Credit'], cmp_ignore_case)
更好的
for i in range(0,10):
if s1[i].lower() > s2.lower():
return 1
break
elif s1[i].lower() < s2.lower():
return -1
break
return 0
print sorted(['bob', 'about', 'Zoo', 'Credit'], cmp_ignore_case)
更好的
2017-12-20
#coding=utf-8
class Person(object):
def __init__(self, name, score):
self.name=name
self._score=score
p = Person('Bob', 59)
print p.name
print 'attributeerror'
#纯属投机取巧,学术不精
class Person(object):
def __init__(self, name, score):
self.name=name
self._score=score
p = Person('Bob', 59)
print p.name
print 'attributeerror'
#纯属投机取巧,学术不精
2017-12-20
import math
def is_sqr(x):
for y in range(1,11):
if x==y*y:
return x
print filter(is_sqr, range(1, 101))
不用sqrt的方法
def is_sqr(x):
for y in range(1,11):
if x==y*y:
return x
print filter(is_sqr, range(1, 101))
不用sqrt的方法
2017-12-20
if num < 0:
pass
elif num == 1:
self.l = [0]
elif num == 2:
self.l = [0, 1]
else :
self.l = [0, 1]
length = len(self.l)
while length < num:
self.l.append(self.l[length-2]+self.l[length-1])
length = length + 1
pass
elif num == 1:
self.l = [0]
elif num == 2:
self.l = [0, 1]
else :
self.l = [0, 1]
length = len(self.l)
while length < num:
self.l.append(self.l[length-2]+self.l[length-1])
length = length + 1
2017-12-20
如果list不仅仅包含 Student 类,则 __cmp__ 可能会报错:
只要在__cmp__函数里加上判断
def __cmp__(self, s):
if isinstance(s, Student) == False:
return 0
if self.name < s.name:
return -1
elif self.name > s.name:
return 1
else:
return 0
就可以了.
只要在__cmp__函数里加上判断
def __cmp__(self, s):
if isinstance(s, Student) == False:
return 0
if self.name < s.name:
return -1
elif self.name > s.name:
return 1
else:
return 0
就可以了.
2017-12-20
def performance(unit):
def wrapper(f):
def fwrapper(*args, **kw):
startMil = getTime(unit)
ff = f(*args, **kw)
print 'call %s() exec time: %d %s' % (f.__name__, getTime(unit)-startMil,unit)
return ff
return fwrapper
def wrapper(f):
def fwrapper(*args, **kw):
startMil = getTime(unit)
ff = f(*args, **kw)
print 'call %s() exec time: %d %s' % (f.__name__, getTime(unit)-startMil,unit)
return ff
return fwrapper
2017-12-19
import os
print os.path.isdir(r'/data/webroot/conf')
print os.path.isfile(r'/data/webroot/conf/app.conf')
这样就成功了
print os.path.isdir(r'/data/webroot/conf')
print os.path.isfile(r'/data/webroot/conf/app.conf')
这样就成功了
2017-12-19
def do(x,y):
return x*y
def calc_prod(lst):
def g():
return reduce(do,lst)
return g
f = calc_prod([1, 2, 3, 4])
print f()
嗯,我觉得我这个写法更适合我们这些凡人
return x*y
def calc_prod(lst):
def g():
return reduce(do,lst)
return g
f = calc_prod([1, 2, 3, 4])
print f()
嗯,我觉得我这个写法更适合我们这些凡人
2017-12-19
import math
def is_sqr(x):
return math.sqrt(x) - int(math.sqrt(x)) == 0
print filter(is_sqr, range(1, 101))
def is_sqr(x):
return math.sqrt(x) - int(math.sqrt(x)) == 0
print filter(is_sqr, range(1, 101))
2017-12-19