MIT 6.00 Intro Computer Science (OCW)
OpenStudy (anonymous):

I recently completed PS2, Q4. The goal is to create a program that solves for highest number of mcnuggets that cannot be bought in exact quantity when the number of mcnuggets in each package can vary (note: maximum number of unsolvable numbers is supposed to stop at 200). I have accomplished this goal (I think). However, I'd really like suggestions as to how to simplify my code. Are there alternative/easier ways to accomplish some of the specific steps? Or is there perhaps a better general methodology for solving the problem? etc. Thanks for the input. http://codepad.org/BHYqzQG8

OpenStudy (anonymous):

OpenStudy (anonymous):

don't write such long lines for commenting, use enter :)

OpenStudy (anonymous):

So I was a bit confused by Q4. I was under the impression that they just wanted us to build a program that spits out the largest exact quantity of mcnuggets that cannot be bought regardless of package size. So really I just adjusted my answer to Q3 so that it's easier to input other package sizes... See for yourself: http://codepad.org/RVf6oHXc

OpenStudy (anonymous):

@sunsunsun1125 and @pcv2: Thanks for sharing the codes. I couldn't figure out how to write the program as a single function, so your examples were very helpful. @Tomas.A: Didn't realize it doesn't automatically return. I'll be sure to shorten for next time.

OpenStudy (anonymous):

@sumsunsum1225's solution is not a solution to Q4. it's a solution to problem 3. he/she does, however, stop appropriately when six consecutive numbers satisfy the equation. @pcv2 arrives at the right answer for Q4. However, he/she is not likely to get full credit because he/she doesn't provide mechanism for stopping when n consecutive solutions are reached. @morgk16. observation about your approach. i like the fact that you are trying to use functions. I wish we were seeing more people use functions to break up the problem into pieces. my suggestion is that you first write a function that does one only one thing: it accepts a number as input and determines whether the input is or is not a solution to the diophantine equation. Here is the skeleton: def diophantine_check(num): # if num satisfies the equation return true # if num does not satisfy the equation return false what you can learn from @sunsunsum and @pcv2 is how they both use three for -loop to cycle through the possible solution space.

OpenStudy (maitre_kaio):

I see one big problem with your code: you are using global variables, i.e variables that are defined outside any function, and thus are accessible to all functions. I know it can seem to be pretty cool when one is starting to program. But this is really a bad habit which you need to lose as soon as possible. I was going to explain it in details, but there is no need since you just have to google 'use global variables bad' to read about it. The wikipedia article is a good start (as always): http://en.wikipedia.org/wiki/Global_variable

OpenStudy (anonymous):

@malpaso: Thanks for the insight. I tried to do that with my "mcnuggets" function, and I think that- if I would have been able to determine if the input was or was not a solution to the diophantine equation as simply as @sunsunsun or @pcv2 did- it would have been a much better code. @matrie_kaio: I'm still pretty ignorant of good programming practices. I had no idea global variables were trouble. Thanks for the tip.

Latest Questions
TheShootingStar100: Hi I' new here I need help earning more medals how do I get more
1 hour ago 11 Replies 2 Medals
TheShootingStar100: How do I earn more fans
4 hours ago 8 Replies 1 Medal
Coperdom: how to do the sum of num
7 hours ago 1 Reply 0 Medals
Coperdom: Help
9 hours ago 0 Replies 0 Medals
Coperdom: Help
7 hours ago 6 Replies 0 Medals
Sailor: stress art again bc ive absolutely had a terrible day
9 hours ago 10 Replies 2 Medals
0mega: Phantom of the Opera drawing
10 hours ago 11 Replies 4 Medals