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

Hello, I've just been doing pset2. i've found it very challenging, but i think i've sorted it. I'm fairly convinced there must be a more elegant way to get the job done, and was hoping someone might be able to give me some feedback. To be honest, i'd be content just to know if the answer is correct! I'd also like to thank everyone here who puts the time and effort into answering people's questions, scouring this forum has been an invaluable tool to me, well done all. http://codepad.org/sAWO1cUy

OpenStudy (anonymous):

Hey - your answer is correct (a relief I know), although it has not yet been generalized to answer the code needed for ps2b.py. My solution to this doesn't minimize the time needed to do this either, so I sympathize. Still, I have a few quick thoughts on the code: * You're testing too many options - you know, for example, that any number past 50 will not be the answer. You are checking to see if 20*3 + 6*0 + 9*0 is a possible answer though - so you can cut down on your ranges (you can do this in the general solution as well). * You should probably just delete the six/nine/twentypack variables. As it stands, they are kind of useless. You can delete those assignment statements and replace twentyPack for n, ninePack for o, and sixPack for p in the rest of the code without changing anything (except for the ranges). * I like your method of searching through results :) - it seems really clever to me. But, you have a problem if you generate triplets (or any odd number of repeating numbers, etc.) of numbers in results. This doesn't happen for 6, 9, 20, but it does happen for 8, 11, 14 (44 is put into results 3 times, as is 58, 60 and some others). This causes your program to run until it makes index += 1 go out of results and returns an error. I think you can solve this problem with a while loop outside of the for loop that will go through and delete these repeating numbers. I hope this helps! Let me know if you have any other question / thanks for posting

OpenStudy (anonymous):

(also, even though i just made these up, if you want to test 8, 11, 14, the answer to this is 45.)

OpenStudy (siberman):

45!!! I can't say that's what i got the first time i plugged those numbers in, but it's working now! I understand what you mean about the redundant variables, i think i just found it a bit strange defining a variable in a 'for' loop, i would have thought it needed to be done elsewhere, then referenced. Anyway, gone and working. You were right about the problems that the same number being input more than twice were causing, and simply replacing that 'if' with a 'while' solved it. Finding the roof of the looping range for each of the three parameters was probably the most conceptually challenging part of this exercise, the course so far even, but i think i've grasped it an applied it correctly. Thanks for your help swdalb http://codepad.org/pZ6BIBSq

OpenStudy (anonymous):

:) glad to help. the new code looks really good. just one minor problem still. I ran into this myself as well doing this exercise, but "for n in range(0,(limit/values[2])-1)" should be "for n in range(0,(limit/values[2])+1)" because you are excluding the possibility of (limit/values[2]) * 2 being an answer with this range. you can also put ((limit/value[x]) +1) in for "for o in range(0,values[1]-1):" and "for p in range(0,values[0]-1):" to cut down on how much you are checking. just a quick fix - overall this looks really good now though.

OpenStudy (anonymous):

Attached below is my code for the general case. Let me know what you think: def solve(x, y, z): noSolution = () solutionFound = False for numNuggets in range(1,200): solutionFound = False for numX in range(0,numNuggets/x+1): for numY in range(0,(numNuggets-numX*x)/y + 1): numRemain = numNuggets - numX*x - numY*y if numRemain%z == 0: numZ = numRemain/z solutionFound = True if not solutionFound: noSolution = noSolution + (numNuggets,) print 'Given package sizes',x,y,z,'the largest number of McNuggets \ that cannot be bought in exact quantity is',noSolution[-1]

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!