This is the solution to the problem I'm working on (Python training), but even the solution gives me an error.
Here is the code:
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] # - 13.39 + 17.5x^2 + 3x^3 + x^4
>>> print compute_deriv(poly) # 35^x + 9x^2 + 4x^3
[0.0, 35.0, 9.0, 4.0]
poly: list of numbers, length > 0
returns: list of numbers
"""
poly_deriv = []
if len(poly) < 2:
return [0.0]
for j in xrange(1, len(poly)):
poly_deriv.append(float(j * poly[j]))
return poly_deriv
This is a solution given to me, but when I use the following code to call a function:
poly1 = (-13.39)
print compute_deriv(poly1)
I get
TypeError: object of type 'float' has no len()
I tried a couple of different things inside the if statement (since this code only breaks when len(poly)is <2;
I tried poly_deriv.append(0.0)and return poly_derivfor example.
source
share