Part A: House Hunti
ng
You have graduated from MIT and now have a great job! You move to the San Francisco Bay Area and
decide that you want to start saving to buy a house. As housing prices are very high in the Bay Area,
you realize you are going to have to save for several years before you can afford to make the down
payment on a house. In Part A, 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 dream 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. You will want your main variables to be floats, so you should cast user inputs to floats.
Your program should ask the user to enter the following variables:
1. The starting annual salary (annual_salary)
2. The portion of salary to be saved (portion_saved)
3. The cost of your dream home (total_cost)
annual_salary = float(input("Enter your annual salary: "))
potion_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: "))
portion_down_payment = 0.25
current_savings = 0
r = 0.04
month = 0
down_payment = total_cost * portion_down_payment
while current_savings < down_payment:
current_savings = current_savings * (1 + r/12) + annual_salary * potion_saved / 12
month += 1
print(f'Number of months: {month}')
Part B: Saving, with a rais
e
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 6
th
month, increase your salary by that percentage. Do the same after the 12
th
month, the 18
th
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 semiannual salary raise (semi_annual_raise)
annual_salary = float(input("Enter your annual salary: "))
potion_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 semiannual raise, as a decimal: "))
portion_down_payment = 0.25
current_savings = 0
r = 0.04
month = 0
down_payment = total_cost * portion_down_payment
while current_savings < down_payment:
current_savings = current_savings * (1 + r/12) + annual_salary * potion_saved / 12
if month > 0 and month % 6 == 0:
annual_salary = annual_salary * (1 + semi_annual_raise)
month += 1
print(f'Number of months: {month}')
Part C: Finding the righ
t
am
ount
to s
av
e awa
y
In Part B, you had a chance to explore how both the percentage of your salary that you save each month
and your annual raise affect how long it takes you to save for a down payment. This is nice, but
suppose you want to set a particular goal, e.g. to be able to afford the down payment in three years.
How much should you save each month to achieve this? In this problem, you are going to write a
program to answer
that question. To simplify things, assume:
1. Your semiannual raise is .07 (7%)
2. Your investments have an annual return of 0.04 (4%)
3. The down payment is 0.25 (25%) of the cost of the house
4. The cost of the house that you are saving for is $1M.
You are now going to try to find the best rate of savings to achieve a down payment on a $1M house in
36 months. Since hitting this exactly is a challenge, we simply want your savings to be within $100 of
the required down payment.
In
ps1c.py
, write a program to calculate the best savings rate, as a function of your starting salary.
You should use
bisection sear
ch
to help you do this efficiently. You should keep track of the number of
steps it takes your bisections search to finish. You should be able to reuse some of the code you wrote
for part B in this problem.
Because we are searching for a value that is in principle a float, we are going to limit ourselves to two
decimals of accuracy (i.e., we may want to save at 7.04% or 0.0704 in decimal – but we are not
going to worry about the difference between 7.041% and 7.039%). This means we can search for an
integer
between 0 and 10000 (using integer division), and then convert it to a decimal percentage
(using float division) to use when we are calculating the
current_savings
after 36 months. By using
this range, there are only a finite number of numbers that we are searching over, as opposed to the
infinite number of decimals between 0 and 1. This range will help prevent infinite loops. The reason we
use 0 to 10000 is to account for two additional decimal places in the range 0% to 100%. Your code
should print out a decimal (e.g. 0.0704 for 7.04%).
Try different inputs for your starting salary, and see how the percentage you need to save changes to
reach your desired down payment. Also keep in mind it may not be possible for to save a down
payment in a year and a half for some salaries. In this case your function should notify the user that it
is not possible to save for the down payment in 36 months with a print statement.
P
lease
mak
e
your
program print results in the f
ormat
shown in the t
est
cases be
low.
Note: There are multiple right ways to implement bisec
t
ion search/number of steps so yo
ur
results may not perfectly m
at
def current_saving(annual_salary,potion_saved,r,month):
"""
输入最初的年薪annual_salary,每6个月涨薪比例semi_annual_raise,每月存储的比例potion_saved,年投资回报率r,存储的月份month
输出存储month月后一共存储的金额current_savings
"""
m = 0
current_savings = 0
while m <= month:
current_savings = current_savings * (1 + r/12) + annual_salary * potion_saved / 12
if m > 0 and m % 6 == 0:
annual_salary = annual_salary * (1 + semi_annual_raise)
m += 1
return current_savings
starting_salary = float(input("Enter the starting salary: "))
semi_annual_raise = 0.07
r = 0.04
portion_down_payment = 0.25
total_cost = 1000000
down_payment = total_cost * portion_down_payment
months = 36
low = 0
high = 10000
g = (low + high) / 2
g_count = 1
potion_saved = round(g/10000,4)
current_savings = current_saving(starting_salary, semi_annual_raise, potion_saved, r, months)
if current_savings < down_payment:
print('It is not possible to pay the down payment in three years.')
else:
while abs(current_savings-down_payment) >= 100:
if current_savings > down_payment:
high = g
else:
low = g
g = (low + high) / 2
g_count += 1
potion_saved = round(g/10000,4)
current_savings = current_saving(starting_salary, semi_annual_raise, potion_saved, r, months)
print(f'Best savings rate: {potion_saved}')
print(f'Steps in bisection search: {g_count}')