MIT 6.00 Intro Computer Science (OCW)
OpenStudy (anonymous):

Just finished Problem Set 4. Everything seemed to run properly, and I can't see any faults with the code. Can you guys/gals give this a quick look at and give me any criticisms or thoughts on things I could have done differently are possibly bugs I overlooked? Thanks! https://gist.github.com/1628010

OpenStudy (anonymous):

OpenStudy (anonymous):

Yes, it's right below my the paragraph. Now that I'm looking at it, the link is kind of hard to see due to the background. Let me repost it here: https://gist.github.com/1628010

OpenStudy (anonymous):

it looks ok.

OpenStudy (anonymous):

Hi, would you mind explaining what "account[year-1]" is doing in your else statement in the testNestEggFixed function please?

OpenStudy (anonymous):

nestEggFixed not testNestEggFixed. Sorry.

OpenStudy (anonymous):

That was prior to me figuring out that I can access the last element in a list with account[-1]. In that particular case, I knew exactly how many elements was in the list, and found the last element by calculating the number of years-1. In other words, I learned a better way midway through the problem set to find the last element in a list, and just didn't go back to change that particular piece of code.

OpenStudy (anonymous):

Thanks. It wasn't a criticism. I had just started that problem and I wasn't sure how to do that part and didn't understand what the code was actually doing. It all makes sense now.

OpenStudy (maitre_kaio):

Just two little things you may improve in your code, the goal is too improve readability and to keep maintenance easy to do: 1. In nestEggFixed for example, you are evaluating several times the expression: salary * save * 0.01 You may evaluate it just one time and keep the result in a variable: newContribution = salary * save * 0.01 This will make the code more concise. Another nice consequence: let's say that later you want to change the way this is calculated (maybe the now save is already a percentage). Instead of changing the formula everywhere, which is error prone, you do it in one single place. 2. in the findMaxExpenses while loop, you evaluate the variables guess and postRetireSavings before the loop and inside the loop. For the same reasons than before, I think it's better to do it just one time, in the while loop. If Python had a 'do...while' construct it would be very easy, but sadly it doesn't as far as I know. So to do this you have to slightly change the code. As I said , these are little things, that means your code is pretty good :)

OpenStudy (anonymous):

Hey Matire, Thanks for the feedback! I went back and made same of the changes you talked about and redid some of the code. https://gist.github.com/1628010 Now, I did something with some of the functions that I'm not sure is good practice. I was able to cut some code out by popping the last element in a list returned by nestEggVariables by directly invoking it on the function itself. While this does save me having to do a savings = savings[-1] kind of thing, I was wondering if something like that is considered kosher, so to speak. I ask this because many moons ago I use to mess around with C, and the one thing that working with C left me was that all because there's a million ways to skin a cat, there's just some ways you shouldn't.