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

PS2.3 and 4- I'd really appreciate any comments / criticism on my code: problem 3: http://codepad.org/xU4way0q problem 4: http://codepad.org/aTKhIl48 Many thanks!

OpenStudy (anonymous):

I think your code looks good, good use of filter() and easy to read code. Maybe adding some docstrings, as it is really good to have some insight into what the function actually do, without reading the code(this is important when you want to check your older code, and many times, you will be confused/lost). Other than that, avoid using input() function, it is fed to eval() first, therefore the conversion is always implicit. Better is int(raw_input()), as it is safer, and more explicit. Honestly, not really a big issue here, but as a tip for the future. If you have interest in it, check this source http://linuxgazette.net/issue83/evans.html for defensive tips in Python(it covers more in depth stuff, like handling exceptions, but it's quite readable/informative).

OpenStudy (anonymous):

Albeit I think input() changed in Python 3.x, I am unsure, but I remember something about raw_input being deprecated, but really, this is not really something to worry about, compared to being unsafe, because of how input() works in Python 2.x

OpenStudy (anonymous):

Your code works because you know the answer is less than 1000. How could you change your code so that it will find the number no matter how big it is? Use the technique they gave in the pdf. Also, in the test on line 8 (in the first post, line 13 in the second), the x%n==0 is redundant. If x==n then x%n must be 0. If x!=n, then the mod test isn't even performed. I think Prof Guttag reveals it much later in the class, but, unless you're explicitly using the list returned by range(), you should use xrange() for iterating (including in the call to filter()). And nuggets() and nuggetsNew() are not descriptive names for the functions, and it's awkward that they return the argument or None. The functions are actually testing to see if that number is buyable, so they should return booleans. Functions that return booleans are typically named a question, like isBuyable(). And nuggetsNew() should probably take the packs tuple as an argument. Overall your code is good. It's easy to read and solves the problem correctly. But there's usually always some way to improve a piece of code. My suggestions are nitpicky, but only because you got all the big stuff right. Now it's just tweaking the small stuff.

OpenStudy (rensun):

Thank you both for the feedback. I'll check out the xrange stuff - have not gotten to it yet.

OpenStudy (rensun):

oh and thanks, bmp, for the defensive link...

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!