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!
MIT 6.00 Intro Computer Science (OCW)
I got my questions answered at brainly.com in under 10 minutes. Go to brainly.com now for free help!
At vero eos et accusamus et iusto odio dignissimos ducimus qui blanditiis praesentium voluptatum deleniti atque corrupti quos dolores et quas molestias excepturi sint occaecati cupiditate non provident, similique sunt in culpa qui officia deserunt mollitia animi, id est laborum et dolorum fuga.
Et harum quidem rerum facilis est et expedita distinctio. Nam libero tempore, cum soluta nobis est eligendi optio cumque nihil impedit quo minus id quod maxime placeat facere possimus, omnis voluptas assumenda est, omnis dolor repellendus.
Itaque earum rerum hic tenetur a sapiente delectus, ut aut reiciendis voluptatibus maiores alias consequatur aut perferendis doloribus asperiores repellat.
did you post a link to your code?
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:
Not the answer you are looking for? Search for more explanations.
Hi, would you mind explaining what "account[year-1]" is doing in your else statement in the testNestEggFixed function please?
nestEggFixed not testNestEggFixed. Sorry.
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.
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.
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 :)
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.