这个题目是一个逆天的题目, 我看是闭包, 满脑子想的都是闭包的
其实大部分人被fs这个数组搞晕了, 给个小例子解释下
fs = ['a', 'b', 'c']
a1, a2, a3 = fs
print a3 -> 结果是 c
fs = ['a', 'b', 'c', 'd']
a1, a2, a3 = fs
print a3 -> 结果编译不通过, 报错
题目中的f1(), f2(), f3() 其实只是把数组fs的值取出来.
其实大部分人被fs这个数组搞晕了, 给个小例子解释下
fs = ['a', 'b', 'c']
a1, a2, a3 = fs
print a3 -> 结果是 c
fs = ['a', 'b', 'c', 'd']
a1, a2, a3 = fs
print a3 -> 结果编译不通过, 报错
题目中的f1(), f2(), f3() 其实只是把数组fs的值取出来.
2016-09-01
import math
def add(x, y, f):
if x*y >=0: #不知道是否math.sqrt()支持对负数开根,所以一律禁止传入负数
return f(x) + f(y)
print add(25, 9, math.sqrt)
def add(x, y, f):
if x*y >=0: #不知道是否math.sqrt()支持对负数开根,所以一律禁止传入负数
return f(x) + f(y)
print add(25, 9, math.sqrt)
2016-09-01
....我直接写的是sqrt,鬼知道还要加上math...我还以为import就是类似include的作用呢...还加个毛线的math.?????
2016-09-01
def count():
fs = []
for i in range(1, 4):
def f(j):
def g():
return j*j
return g
fs.append(f(i))
return fs
fs = []
for i in range(1, 4):
def f(j):
def g():
return j*j
return g
fs.append(f(i))
return fs
2016-09-01
import math
def is_sqr(x):
r=math.sqrt(x)
return math.ceil(r)==r
print filter(is_sqr, range(1, 101))
def is_sqr(x):
r=math.sqrt(x)
return math.ceil(r)==r
print filter(is_sqr, range(1, 101))
2016-08-31
def __cmp__(self, s):
if self.score>s.score:
return -1
elif self.score<s.score:
return 1
else:
return (self.name<s.name)and -1 or 1
if self.score>s.score:
return -1
elif self.score<s.score:
return 1
else:
return (self.name<s.name)and -1 or 1
2016-08-31
def cmp_ignore_case(s1, s2):
u1 = s1.upper()
u2 = s2.upper()
if u1 < u2:
return -1
if u1 > u2:
return 1
return 0
print sorted(['bob', 'about', 'Zoo', 'Credit'], cmp_ignore_case)
u1 = s1.upper()
u2 = s2.upper()
if u1 < u2:
return -1
if u1 > u2:
return 1
return 0
print sorted(['bob', 'about', 'Zoo', 'Credit'], cmp_ignore_case)
2016-08-31
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-31
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-31
def__int__(self,name):
self.name=name
这块看不懂,求大神解释
self.name=name
这块看不懂,求大神解释
2016-08-31