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

Does anyone know how to do problem 3 in pset2?

OpenStudy (turingtest):

Welcome to OpenStudy! could you please link to the pset? I'm afraid nobody here has that course memorized, despite the fact that we have a section for it.

OpenStudy (anonymous):

Are you referring to the Newtonian Method problem? I just did it. Please let me know if you need direction.

OpenStudy (turingtest):

The idea I have is to have a loop that increments the number of nuggets starting from zero and keeps checking if that number of nuggets can be created from multiples of 6, 9, and 20. You will need a counter that keeps track of how many quantities in a row can be bought in those multiples, and a variable to store the largest number of nuggets so far that cannot be bought. When you find six quantities in a row that can be bought, the last number that could not be bought is the largest number that cannot be bought, as per the aforementioned theorem in the pset.

OpenStudy (turingtest):

something like ``` #initialize number of nuggets and counter #while we can't buy six quantities in a row #if we can buy this quantity #increment the counter #otherwise #start counter over #store that number of nuggets in a variable #increment the number of nuggets #return last number of nuggets stored ```

OpenStudy (anonymous):

So I did number 3, but for number 4, it gives me ('Given package sizes', 6, 9, 'and', 20, ', the largest number of McNuggets that cannot be bought in exact quantity is', 43) when I printed it. Here is my code: def max_not_purchasable(x, y, z): packages=[x, y, z] max_number=x count=0 n=x while count<x: n+=1 ans=False for a in range((n/x)+1): for b in range((n/y)+1): for c in range((n/z)+1): if x*a+y*b+z*c==n: ans=True if ans: count+=1 else: count=0 max_number=n return "Given package sizes", x, y, "and", z, ", the largest number of McNuggets that cannot be bought in exact quantity is", max_number print max_not_purchasable(6, 9, 20) also, I wrote this code in AptanaStudio just because I find it easier to test my code while writing in this editor. But when I copied the code to idle and press f5, it said I got syntax error but the code worked just fine in AptanaStudio. Any idea why? Thanks a lot for your reply

OpenStudy (turingtest):

do you know what the answer is supposed to be?

OpenStudy (turingtest):

I checked, and it seems 43 is correct. I have no idea why that code did not work in IDLE, I just did it and it worked fine.

OpenStudy (anonymous):

That was because I put in code for the whole pset 2 but in AptanaStudio it worked fine though

OpenStudy (turingtest):

well i just put in the code you posted above, and it worked just fine i'm a bit too lazy to play around with pasting all of pset 2 into my IDLE and trying to figure it out, sorry :P

OpenStudy (turingtest):

unless you want to just paste what you put into your IDLE here

OpenStudy (anonymous):

Well, when IDLE said invalid syntax it highlighted "7" in the "Python 2.7.9 (default, Dec 10 2014, 12:24:55) [MSC v.1500 32 bit (Intel)] on win32 Type "copyright", "credits" or "license()" for more information.". Does it normally not highlight the line that is wrong? does it mean that something is wrong with the IDLE I have on my computer?

OpenStudy (turingtest):

did you restart the shell and everything?

OpenStudy (anonymous):

Yes, I did

OpenStudy (turingtest):

did you paste it into a new window that you opened with crt+n ?

OpenStudy (turingtest):

http://prntscr.com/5p20po working fine for me

OpenStudy (anonymous):

No I just copied straight onto IDLE. I tried crt+n, it worked fine for me. But any idea why the last line is in bracket and also ' ' ?

OpenStudy (turingtest):

It's because you called `print` on the function call `max_not_purchasable(6, 9, 20)`. Usually you would choose between printing that string at the end instead of returning it, not using a function like you did for the previous problems, or simply returning the string ``` def max_not_purchasable(x, y, z): packages=[x, y, z] max_number=x count=0 n=x while count<x: n+=1 ans=False for a in range((n/x)+1): for b in range((n/y)+1): for c in range((n/z)+1): if x*a+y*b+z*c==n: ans=True if ans: count+=1 else: count=0 max_number=n return "Given package sizes", x, y, "and", z, ", the largest number of McNuggets that cannot be bought in exact quantity is", max_number max_not_purchasable(6, 9, 20) ```

OpenStudy (anonymous):

I see, thanks a lot

OpenStudy (turingtest):

welcome!

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!