Python 作业菜鸟求答案在线等
defseries(f,n):"""Computesthesumoftheseriesdefinedbyfunctionfforthefirst1throughnterm...
def series(f, n):
"""
Computes the sum of the series defined by function f
for the first 1 through n terms
f -- function
n -- number
returns -- number
"""
def harmonic(n):
return 1.0 / n
def geometric_onehalf(n):
return (1.0 / 2.0) ** (n-1)
def alternating_harmonic(n):
return (2 * (n % 2) - 1) * harmonic(n)
assertEqual(series(harmonic, 1), 1.0)
assertEqual(series(harmonic, 10), 2.9289)
assertEqual(series(geometric_onehalf, 2), 1.5) 展开
"""
Computes the sum of the series defined by function f
for the first 1 through n terms
f -- function
n -- number
returns -- number
"""
def harmonic(n):
return 1.0 / n
def geometric_onehalf(n):
return (1.0 / 2.0) ** (n-1)
def alternating_harmonic(n):
return (2 * (n % 2) - 1) * harmonic(n)
assertEqual(series(harmonic, 1), 1.0)
assertEqual(series(harmonic, 10), 2.9289)
assertEqual(series(geometric_onehalf, 2), 1.5) 展开
1个回答
2010-10-25
展开全部
def series(f, n):
"""
Computes the sum of the series defined by function f
for the first 1 through n terms
f -- function
n -- number
returns -- number
"""
return sum(f(i) for i in range(1,n+1))
"""
Computes the sum of the series defined by function f
for the first 1 through n terms
f -- function
n -- number
returns -- number
"""
return sum(f(i) for i in range(1,n+1))
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询