Quantcast

Got Homework?

Connect with other students for help. It's a free community.

  • across
    MIT Grad Student
    Online now
  • laura*
    Helped 1,000 students
    Online now
  • Hero
    College Math Guru
    Online now

Here's the question you clicked on:

55 members online
  • 0 replying
  • 0 viewing

David_Novo

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/

  • one year ago
  • one year ago

  • This Question is Closed
  1. shandelman
    Best Response
    You've already chosen the best response.
    Medals 3

    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.

    • one year ago
  2. David_Novo
    Best Response
    You've already chosen the best response.
    Medals 0

    Thanks for the great reply.

    • one year ago
  3. PierreHardy
    Best Response
    You've already chosen the best response.
    Medals 0

    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.

    • one year ago
  4. shandelman
    Best Response
    You've already chosen the best response.
    Medals 3

    @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.

    • one year ago
    • Attachments:

See more questions >>>

Your question is ready. Sign up for free to start getting answers.

spraguer (Moderator)
5 → View Detailed Profile

is replying to Can someone tell me what button the professor is hitting...

23

  • Teamwork 19 Teammate
  • Problem Solving 19 Hero
  • You have blocked this person.
  • ✔ You're a fan Checking fan status...

Thanks for being so helpful in mathematics. If you are getting quality help, make sure you spread the word about OpenStudy.

This is the testimonial you wrote.
You haven't written a testimonial for Owlfred.