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
don't write such long lines for commenting, use enter :)
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
@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.
@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.
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
@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.
Join our real-time social learning platform and learn together with your friends!