Description of this assignment:

Description of this assignment: — page 1
Description of this assignment: — page 1

Download original PDF: ## Description of this assignment: — page 1

Description of this assignment: — page 2
Description of this assignment: — page 2
Description of this assignment: — page 3
Description of this assignment: — page 3
Description of this assignment: — page 4
Description of this assignment: — page 4
Description of this assignment: — page 5
Description of this assignment: — page 5
Description of this assignment: — page 6
Description of this assignment: — page 6

L2_HW.pdf

My Code:

Part A/B

total_cost = float(input("How much is the total cost of your dream home?"))
annual_salary = float(input("How much is your annual salary?"))
portion_saved = float(input("How much portion would you save for each month?"))
semi_annual_raise = float(input("How much portion would the company raise for each 6 months?"))

portion_down_payment = 0.25
current_savings = 0
r = 0.04
month = 0
while current_savings<total_cost*portion_down_payment:
    if (month%6 == 0) and (month > 0):
        annual_salary = annual_salary*(1+semi_annual_raise)
    earn_monthly = (current_savings*r)/12 
    current_savings=current_savings+earn_monthly+((annual_salary/12)*portion_saved)
    month += 1
print("Number of month:", month)

Part C (linear search)

total_cost = 1000000
semi_annual_raise = 0.07
portion_down_payment = 0.25
r = 0.04


annual_salary = float(input("How much is your annual salary?"))
steps = 0

# test each portion_saved 1-by-1
for portion_saved in range(10001):
    
    current_savings = 0
    month = 0
    annual_salary_reset = annual_salary
    
    # calculate total savings # 注意這邊是要用 < 36 
    while month < 36:
        if (month%6 == 0) and (month > 0):
            annual_salary_reset = annual_salary_reset*(1+semi_annual_raise)
        earn_monthly = (current_savings*r)/12 
        current_savings = current_savings + earn_monthly + ((annual_salary_reset/12)*portion_saved/10000)
        month += 1
    # check if the total saving is enough?
    if current_savings >= total_cost*portion_down_payment:
        print("best saving rates:", portion_saved/10000) 
        break
    15
    steps += 1
    
    if portion_saved == 10000:
        print("It is not possible to pay the down payment in three years.")
        break

print("Steps in linear search:", steps)

Part C (bisection search)

# variables
total_cost = 1000000
portion_down_payment = 0.25
goal_savings = total_cost*portion_down_payment
r = 0.04
semi_annual_raise = 0.07

# boundary
portion_saved_low = 0
portion_saved_high = 10000
portion_saved = (portion_saved_low + portion_saved_high)/2

# initiate value
bisection_time = 1
annual_salary = 150000
current_savings = 0


while abs(current_savings - goal_savings) > 100 and bisection_time < 10000:
        
    # total savings
    annual_salary_reset = annual_salary
    month = 0
    current_savings = 0
    while month < 36:
        if (month%6 == 0) and (month > 0):
            annual_salary_reset = annual_salary_reset*(1+semi_annual_raise)
        earn_monthly = (current_savings*r)/12 
        current_savings = current_savings + earn_monthly + ((annual_salary_reset/12)*portion_saved/10000)
        month += 1
            
    # bisection search: change boundary of portion_saved
    if current_savings > goal_savings:
        portion_saved_high = portion_saved
    else:
        portion_saved_low = portion_saved
    portion_saved = (portion_saved_low + portion_saved_high)/2
    
    # add bisection searching time
    bisection_time += 1


print("bisection time:",bisection_time)
if bisection_time < 10000:
    print("saving rate:", portion_saved/10000)
else:
    print("it is not possible to reach the goal saving with this annual salary.")
← Back to all posts