def noah(x):
return x*x*x
def summer(n, f):
"""precondition: n is a nonnegative number and f is a function
whose domain includes the nonnegative integres
postcondition: returns sum( f(k), k = 1..n) """
y = 0
for i in range (1, n + 1):
y = y + f(i)
return y
递归是你的朋友。你知道吗
另一种方法是用f映射范围列表:
嗯。。。使用发电机似乎更容易:
相关问题 更多 >
编程相关推荐