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

I'm on ps2 trying to create a function that computes the derivative of a polynomial. This is what I have so far for the block of code. sumPoly = 0 for ind in range(len(poly)): sumPoly = sumPoly + (poly[ind]*(ind)*x**(ind-1) print sumPoly

OpenStudy (anonymous):

ok number one, all your doing here is adding and multiplying integers with the string 'x'. the problem asks for derivatives, are you familiar with them? because what your doing is adding together all the derivatives even though you don't know the value of 'x'. maybe you should think about the places in which 'x' is located in 'poly' as the power of 'x' so what they are saying in the problem is lower its index by one. my solution would be to create a list and then turn it into a tuple because if i remember the problem specifically want you to return a tuple. and everything located inside the index is 'poly[ind'] *ind. try this out and let me know if you dont understand.

OpenStudy (anonymous):

I understand derivatives, I just haven't got the hang of coding yet, clearly. I thought in the problem, the variable ind represented the index of each element, which corresponds to the power correct? So i was trying to multiply the coefficient of x --> poly[ind] and the power corresponding to that number --> ind together. Then i was leaving x as an unknown and raising it to the power minus 1 represented by --> x**(ind - 1) That probably doesn't make sense to you because I think there is a flaw in my logic somewhere. I'm going to try the to put it into a list though and see what happens i guess

OpenStudy (anonymous):

i see what you're trying to do, and it's not so much a flaw in logic its that you'r forgetting something --> here's a hint: is it possible to add 7 and 'hello' and what would the answer be? but other than that i think you got it.

OpenStudy (anonymous):

it is possible if you make 7 a string. you get 7hello. So you're saying I need to make part of my code into a string?

OpenStudy (anonymous):

yes that would be the way to do it but that's not what I'm saying. what I'm saying is that 'x' isn't supposed to be in your calculation portion of the code. the 'x' ( if you insist on adding it ) would have to be added at the end. something like this DON'T READ IF YOU DON'T WANT AN ANSWER: ex: ##this would include whatever initialization you had in your original code newList = [] #creates an empty list for ind in range( len(poly)): if ind == 0: continue # skips the iteration else: a_ind = poly(ind)"ind #a*b in the problem set newList.append( str( a_ind) +'x') # you might have to find a way to represent the power if ###you want but for now the location in 'newList' is fine. ans = tuple( newList) print ans

OpenStudy (anonymous):

god I'm really bad at this. thanks for your help. I finally figured it out. (sort of)

OpenStudy (anonymous):

no problem. and don't worry about it it will become much easier after the first few programs you write.. plus once you know this for python, the basic rules apply to most other languages so its not like you will have to learn the entire thing over again. So don't give up! and what do you mean sort of? if you still want to work on it just post your code and maybe i can help. Oh btw its much easier for everyone if you use a site like dpaste.com to post your program so it doesn't clog up the post( i used to do the same until some one told me about it)

OpenStudy (anonymous):

ok thanks. I got it but I can never figure it out on my own. I always end up basically using someone else's code so i dont even know if I'm getting any better. Its frustrating

OpenStudy (anonymous):

ah yeah that's a really crummy( because i don't want to be vulgar) feeling but here is the question you should be asking yourself: Are you understanding everything the professor is saying and all the codes you've been using? if yes then your fine its just the beginning of learning the language and how to use it so don't worry just keep at it and don't make it a habit of asking on this forum without trying. The same goes for if you answered no. it'll come don't worry.

OpenStudy (anonymous):

I have been understanding almost everything I just cant come up with it on my own. But yeah hopefully it'll come with time

OpenStudy (anonymous):

It's like learning a new ('human' ) language. You understand stuff but when it comes to talking it's a big pain in the behind, but after a while you pick it up and it becomes easier right?

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!
Latest Questions
danielfootball123: .
2 hours ago 6 Replies 0 Medals
Graysonunderwoods: Feelings ig
52 minutes ago 4 Replies 2 Medals
Breathless: Tada!!!! NEW START!
7 hours ago 2 Replies 2 Medals
luhbabyliyahh: I am looking for a theater script to write can someone help me?
2 hours ago 11 Replies 0 Medals
danielfootball123: What is the name of the president who got assassinated?
57 minutes ago 20 Replies 3 Medals
MAGABACK: ART!
12 hours ago 5 Replies 0 Medals
danielfootball123: Is Donald trump a good president?
14 hours ago 92 Replies 6 Medals
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!