Ask your own question, for FREE!
MIT 6.00 Intro Computer Science (OCW) 24 Online
OpenStudy (anonymous):

Could anyone offer help on #1 on PS2? This is what I have so far. Is the index part ok? def evaluate_poly(poly, x): ##Computes the polynomial function for a given value x. Returns that value. for index in poly[-1] return evaluate_poly(poly, x) ##Example: # f(x) = 7x^4 + 9.3x^3 + 5x^2 # f(-13) = 7(-13)^4 + 9.3(-13)^3 + 5(-13)^2 =180339.9 poly = (0.0, 0.0, 5.0, 9.3, 7.0) # f(x) = 7x^4 + 9.3x^3 + 5x^2 x = -13 print evaluate_poly(poly, x) ##poly: tuple of numbers, length > 0 ##x: number ##returns: float # TO DO ...

OpenStudy (anonymous):

If you write poly[-1], then you are only passing it one value to iterate over. For example: poly = (0.0, 0.0, 5.0, 9.3, 7.0) In this case poly[-1] => 7.0 You want it to iterate over a range of values. So my hint here is to use the function called xrange. The xrange documentation says this: xrange([start, ] stop [, step])

OpenStudy (anonymous):

in case you still need help, i was finally able to complete this by assigning "index" the value of the length of the tuple "poly" -1. len(poly) - 1. this gives the polynomial the correct exponent based on the length of the tuple. you can use a loop to count the indices down while adding them together. no idea if this is the best way to do it, but it works.

OpenStudy (anonymous):

just looked at the solution. same results only much less code than mine. LOL

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!