Programming Project 4

Programming Projects are to be submitted to gradescope.

Due date: Oct 7, Monday at 7pm

This assignment is an adaptation of the MIT’s 6.0001 Introduction to Computer Science and Programming in Python programming set 1.

Make sure you follow the provided style guide (you will be graded on it!).

Also make sure you check the Academic Integrity and Common Gradescope Errors pages.

House Hunting

You have graduated and now have a great job and decide that you want to start saving to buy a house. As housing prices are very high in the US right now, you realize you are going to have to save for several years before you can afford to make the down payment on a house.

Part 1

Name the program house_hunting_1.py. Make sure that gradescope gives you the points for passing the test cases.

I this assignment we are going to determine how long it will take you to save enough money to make the down payment given the following assumptions:

  1. Call the cost of your first home total_cost.
  2. Call the portion of the cost needed for a down payment portion_down_payment . For simplicity, assume that portion_down_payment = 0.25 (25%).
  3. Call the amount that you have saved thus far current_savings. You start with a current savings of $0.
  4. Assume that you invest your current savings wisely, with an annual return of r (in other words, at the end of each month, you receive an additional current_savings*r/12 funds to put into your savings – the 12 is because r is an annual rate). Assume that your investments earn a return of r = 0.04 (4%).
  5. Assume your annual salary is annual_salary .
  6. Assume you are going to dedicate a certain amount of your salary each month to saving for the down payment. Call that portion_saved. This variable should be in decimal form (i.e. 0.1 for 10%).
  7. At the end of each month, your savings will be increased by the return on your investment, plus a percentage of your monthly salary (annual salary / 12).

Write a program to calculate how many months it will take you to save up enough money for a down payment.

Test cases – Part 1

Try different arguments and see how long it takes to save for a down payment. Please make your program print results in the format shown in the test cases below.

Test Case 1

annual_salary = 120000
portion_saved = .1
total_cost = 1000000
total_months = calculate_months(annual_salary, portion_saved, total_cost)
print("Number of months:", total_months)
Number of months: 183

Test Case 2

annual_salary = 80000
portion_saved = .15
total_cost = 500000
total_months = calculate_months(annual_salary, portion_saved, total_cost)
print("Number of months:", total_months)
Number of months: 105

Part 2

Name the program house_hunting_2.py. Make sure that gradescope gives you the points for passing the test cases.

In Part 1, we unrealistically assumed that your salary didn’t change. So we are going to build on your solution to Part 1 by factoring in a raise every six months.

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

  1. Use a variable for 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 12th 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%).

Be careful about when you increase your salary – this should only happen after the 6th, 12th, 18th month, and so on.

Test cases – Part 2

Try different arguments and see how long it takes to save for a down payment. Please make your program print results in the format shown in the test cases below.

Test Case 1

annual_salary = 120000
portion_saved = .05
total_cost =  500000
semi_annual_raise = .03
total_months = calculate_months(annual_salary, portion_saved, total_cost, semi_annual_raise)
print("Number of months:", total_months)
Number of months: 142

Test Case 2

annual_salary =  80000
portion_saved = .1
total_cost =   800000
semi_annual_raise = .03
total_months = calculate_months(annual_salary, portion_saved, total_cost, semi_annual_raise)
print("Number of months:", total_months)
Number of months: 159

Test Case 3

annual_salary =   75000
portion_saved = .05
total_cost =    1500000
semi_annual_raise = .05
total_months = calculate_months(annual_salary, portion_saved, total_cost, semi_annual_raise)
print("Number of months:", total_months)
Number of months: 261