Tuesday, January 22, 2019

MIT Intro to CS and Programming PS1B

Here is the solution to problemset1 question B of 6.0001 entitled Introduction to Computer Science and Programming in Python (Fall 2016).

Problem

Part B: Saving, with a raise
Background  
In Part A, we unrealistically assumed that your salary didn’t change.  But you are an MIT graduate, and clearly you are going to be worth more to your company over time! So we are going to build on your solution to Part A by factoring in a raise every six months.  

In ps1b.py, copy your solution to Part A (as we are going to reuse much of that machinery).  Modify your program to include the following

  1. Have the user input a semi-annual salary raise semi_annual_raise (as a decimal percentage) 
  2. After the 6th month, increase your salary by that percentage. Do the same after the 12 th th month, the 18 month, and so on.
Write a program to calculate how many months it will take you save up enough money for a down payment.  Like before, assume that your investments earn a return of r = 0.04 (or 4%) and the required down payment percentage is 0.25 (or 25%).  Have the user enter the following variables:

  1. The starting annual salary (annual_salary) 
  2. The percentage of salary to be saved (portion_saved) 
  3. The cost of your dream home (total_cost) 
  4. The semi­-annual salary raise (semi_annual_raise)
Hints To help you get started, here is a rough outline of the stages you should probably follow in writing your code:

  • Retrieve user input. 
  • Initialize some state variables. You should decide what information you need. Be sure to be careful about values that represent annual amounts and those that represent monthly amounts.
  • Be careful about when you increase your salary – this should only happen after the 6th, 12 th, 18 th month, and so on.
Try different inputs and see how quickly or slowly you can save enough for a down payment.  Please make your program print results in the format shown in the test cases below.

Test Case 1
>>>   
Enter your starting annual salary: 120000
Enter the percent of your salary to save, as a decimal: .05
Enter the cost of your dream home: 500000
Enter the semi­annual raise, as a decimal: .03 Number of months: 142
>>>

Test Case 2  
>>>   
Enter your starting annual salary: 80000
Enter the percent of your salary to save, as a decimal: .1
Enter the cost of your dream home: 800000
Enter the semi­annual raise, as a decimal: .03
Number of months: 159  
>>>

Test Case 3  
>>>   
Enter your starting annual salary: 75000
Enter the percent of your salary to save, as a decimal: .05
Enter the cost of your dream home: 1500000
Enter the semi­annual raise, as a decimal: .05
Number of months: 261
>>>

(Bell, Grimson & Guttag, 2016)

Solution
The solution to this problem is essentially the same as the solution to problem set A with one small change. They say that you are getting a raise every six months.

You can easily calculate the raise by adding the extra money to the salary variable, but you need to do this only every six months.

The % operator gives returns remainders. Whenever the remainder of 6 is zero you are going to know that that number is a multiple of 6. You can add this condition in as an if statement into the while loop.

Code

annual_salary = float(input("Enter your annual salary: "))
portion_saved = float(input("Enter the percent of your salary to save, as a decimal: "))
total_cost = float(input("Enter the cost of your dream home: "))
semi_annual_raise = float(input("Enter the semi­annual raise, as a decimal: "))

portion_down_payment = total_cost * 0.25
current_savings = 0
r = 0.04
months = 0
while current_savings < portion_down_payment:
   current_savings += (annual_salary/12)*portion_saved + current_savings*(r/12)
   months += 1
   if months%6 == 0:
       annual_salary += annual_salary * semi_annual_raise

print("Number of months: ", months)

Output

Test case 1
Enter your annual salary: 120000
Enter the percent of your salary to save, as a decimal: .05
Enter the cost of your dream home: 500000
Enter the semi­annual raise, as a decimal: .03
Number of months:  142

Test case 2
Enter your annual salary: 80000
Enter the percent of your salary to save, as a decimal: .1
Enter the cost of your dream home: 800000
Enter the semi­annual raise, as a decimal: .03
Number of months:  159

Test case 3
Enter your annual salary: 75000
Enter the percent of your salary to save, as a decimal: .05
Enter the cost of your dream home: 1500000
Enter the semi­annual raise, as a decimal: .05
Number of months:  261

Reference
Ana Bell, Eric Grimson, and John Guttag. 6.0001 Introduction to Computer Science and Programming in Python. Fall 2016. Massachusetts Institute of Technology: MIT OpenCourseWare, https://ocw.mit.edu. License: Creative Commons BY-NC-SA.

2 comments:

  1. Thanks for the code insight...did you move your blog to somewhere else? Very useful as I'm going through the MIT open courseware.

    ReplyDelete
  2. I just applied for both an individual loan and an entitled Loan from Mr Pedro Jerome and his loan organization at the 3% rate I will be starting my loan repayment next month and it will really be helpful to anyone here looking for a loan to contact Mr Pedro Jerome on pedroloanss@gmail.com +1-863-231-0632 whatsapp text for individual or entitle loans with a simple loan repayment as mine.

    ReplyDelete

MIT Intro to CS and Programming PS1B

Here is the solution to problemset1 question B of 6.0001 entitled Introduction to Computer Science and Programming in Python (Fall 2016). ...