所以,我在麻省理工学院开放式课程《计算机科学与编程导论》中做了一个问题:Problem #2
Implement the compute_deriv function. This function computes the derivative
of a polynomial function. It takes in a tuple of numbers poly and returns
the derivative, which is also a polynomial represented by a tuple.
def compute_deriv(poly):
"""
Computes and returns the derivative of a polynomial function. If the
derivative is 0, returns (0.0,).
Example:
>>> poly = (-13.39, 0.0, 17.5, 3.0, 1.0) # x4 + 3.0x3 + 17.5x2 - 13.39
>>> print compute_deriv(poly) # 4.0x3 + 9.0x2 + 35.0x
(0.0, 35.0, 9.0, 4.0)
poly: tuple of numbers, length > 0
returns: tuple of numbers
"""
# TO DO ...
这是我的程序(有效):
^{pr2}$
首先,我想让程序要求我输入多项式,而不是把它写在里面:...
polyx=tuple(raw_input("Enter your polynomial tuple here:"))
print compute_deriv(polyx)
但那没用:Enter your tuple here:-13.39, 0.0, 17.5, 3.0, 1.0
('1', '33', '...', '33', '99999', ',,,,,,', ' ', '00000000', '...',
'00000000', ',,,,,,', ' ', '1', '77777777777777', '...',
'5555555555555555', ',,,,,,', ' ', '33', '...', '00000000', ',,,,,,',
' ', '1', '...', '00000000')
为什么?
另一个问题是第二个元组(-8x^5+2.5x^3+4x^2+7x+1.3)-当其成员分别为(1.3、7.0、4.0、2.5、0.0、-8.0)时,它会按预期返回-(7.0、8.0、7.5、0.0、-40.0),但如果第一个元组是0.0(如-8x^5+2.5x^3+4x^2+7x),则会发生变化-(7.0、8.0、7.5、-40.0)。第二个0.0被省略了,这是一个问题,因为它意味着-40.0的幂是3,当它是4时。再说一遍,为什么?在
感谢您抽出时间!在