Ask your own question, for FREE!
MIT 6.00 Intro Computer Science (OCW) 11 Online
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):

http://codepad.org/UNoDl7wD hope that will help you. it is what I did

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.

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
laylasnii13: Can i dm anybody to vent having a rough time ??
8 hours ago 1 Reply 0 Medals
kaelynw: starting to draw a hand
7 hours ago 15 Replies 2 Medals
Twaylor: Rate it :D (Took 2 days)
8 hours ago 7 Replies 0 Medals
XShawtyX: Art, Short Writing Assignment: Imagining Landscapes
8 hours ago 4 Replies 1 Medal
XShawtyX: Chemistry, Help ud83dude4fud83cudffe
1 day ago 13 Replies 1 Medal
kaelynw: tried a lil smt, the arm is off but i like the other stuff
1 day ago 27 Replies 3 Medals
kaelynw: art igg
1 day ago 14 Replies 1 Medal
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!