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

Here is how I solved Prob Set 1A. After looking around I can see my approach was different from most. Please take a look and I appreciate any feedback! **Note commented out dynamic request to make the output work for codepad http://codepad.org/tH5ugiBD

OpenStudy (anonymous):

First, my two cents on style(a bit nit-picky perhaps): using global variables should be avoided as much as possible(in this case, for instance, it could be easily be put on functions/be declared locally). The second thing that draws my attention is that you didn't define a function for primality; i.e., that chunk of code could be put into 1-2 functions, this will help both debugging as it increases readability. Lastly, I don't think printing "n is a prime\n" is good, it could easily flood your IDLE or IDE and you can't analyze the output easily. I will analyze more this code, will post more things if I catch them.

OpenStudy (anonymous):

bmp: While I agree that functions are the way to go, I think the assignment and the course actually expects an answer more like this than most peoples (mine included).

OpenStudy (anonymous):

bkochendorfer: Welcome to the group and congrats on solving the problem. As bmp noted, there are better ways to solve the problem, but your solutions works! So congrats.

OpenStudy (anonymous):

Another minor thing, you don't need to check for value comparison for booleans in Python. Instead of "if isPrime == True" you can write if isPrime or if you need the negation, if not isPrime; makes your code more 'English', but, anyway, nit-picky changes. The big point, as seanlerner wrote, is that your solution works :-), congrats

OpenStudy (anonymous):

bmp & seanlerner thank you both for the tips and advice. I cleaned it up a bit based on your feedback. I'll keep the prints for now as it's kind of fun. Your feedback was truly useful. Round 2 : http://codepad.org/4goh7Y3G

OpenStudy (anonymous):

Your code looks a lot cleaner now, last two thing I will throw in: import statements are usually put in the top of the file, kinda like headers in C... A convention I guess. The other thing, that I think is more important, is that you should truncate or int the math.sqrt(n), because you are actually assuming that Python compares float with int efficiently and that there is an implicit conversion. That might work, but it's generally a bad programming discipline, as you are expecting a lot from the language itself.

OpenStudy (anonymous):

I would just add that we hadn't learned about functions yet when doing ps1 (if you're following the syllabus). So while it may not be ideal programming to do it without a function, it's all we could be expected to do at first. Congrats to bkochendorfer for successfully completing it!

OpenStudy (anonymous):

Looking forward to completing more of these. Seems like a great community here. This is my answer to the second problem http://codepad.org/rHHvQLE1

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!