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

Problem Set 1 - Problem 1 - MIT 6.00 Spring 2011 Is the following code less efficient than the one in the solutions? I'm wondering this because it does the same steps but in more lines of code. http://dpaste.com/773504/

OpenStudy (anonymous):

Efficiency is not actually based on the number of lines of code, it's based on how the time it takes to run the program based on your input. I could write a program in 100 lines that you could write in 2, and they might still be about as efficient. To answer your question, your code is pretty much as efficient as the solution's code. It enters one for loop that runs based on the number of months (in this case, always 13) and then ends. Since you can't change the number of months, the length of time this program will run is constant. If you could change the number of months, there would be a linear relationship between how long the program runs and the months (if you multiplied the months by 1000, the problem would take 1000 times longer to run). The solution code is the same way. Is "more" lines of code worse than "fewer" lines of code? Depends. Ultimately, you want your code to be readable by others. Some programmers take pride in the "elegance" of their solution, which means they were able to do it in the fewest lines of code...but elegance is meaningless if no one can understand what you've done. So I wouldn't worry about it too much.

OpenStudy (anonymous):

Thanks for the great reply.

OpenStudy (anonymous):

Your code seems just fine. I do have to comment the shandelman's last paragraph. In IMHO, the 'elegance' of the code is found in it's ability to do what it's supposed to do in the least amount of lines. This will not make it unreadable. Especially if the coder has made proper use of comments.

OpenStudy (anonymous):

@PierreHardy: I can imagine situations where a whole bunch of commands are strung together on one line when they would be more readily understood on separate lines. The method is the same, but trying to condense them all onto one line makes the code more confusing. There is a point where the quest for short code actually hinders elegance.

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
unknownnnnnn: Static at 2 A.M. My mind doesnu2019t knock. It rearranges the furniture at 2 a.m., asks me to notice every creak. I lie still like that might help, like silence is a language my thoughts forgot. They line up with receipts, proof of moments I replayed too many times to pretend they were accidents. Iu2019m fluent in overthinking itu2019s the only subject I never skipped. I can turn one sentence into a courtroom drama, cross-examine my tone, convict myself without witnesses. People call me u201cstrongu201d because I donu2019t spill. They donu2019t see the cup shaking in my hands, how much effort it takes to keep the surface calm. Confidence comes in phases. Some days it fits like skin. Some days itu2019s a costume I forget Iu2019m wearing until it starts to pinch. I laugh on cue. I answer u201cfineu201d with convincing timing. Iu2019ve learned where to pause, how long eye contact should last, how not to sound like a question when Iu2019m one. The past isnu2019t loud. It doesnu2019t need to be. It just clears its throat at the wrong moments, reminds me what I already survived and what might try again. But hereu2019s the part I donu2019t downplay I stay. Even when my thoughts argue in circles, even when doubt files appeals. I choose presence over perfection. Breath over escape. I donu2019t win every round, but I donu2019t forfeit myself either. I am not the static. I am the one listening, deciding what deserves a response and what can fade without taking my name with it.
6 hours ago 2 Replies 0 Medals
addison123456: ignoring everyone cuz idk how to turn off messages>>
16 hours ago 2 Replies 1 Medal
uknownprttyfacekayla: Wrote another poem called " unseen, not unbroken" feedback plz
5 hours ago 12 Replies 2 Medals
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!