Python 3.x 试图使二等分搜索工作,但
我遵循麻省理工学院开放式课程6.0001,在习题集1第c部分中,我们被要求设置二等分搜索,以便找到最佳储蓄率,但我的代码没有按照我的预期工作。需要帮助来解决这个问题Python 3.x 试图使二等分搜索工作,但,python-3.x,bisection,Python 3.x,Bisection,我遵循麻省理工学院开放式课程6.0001,在习题集1第c部分中,我们被要求设置二等分搜索,以便找到最佳储蓄率,但我的代码没有按照我的预期工作。需要帮助来解决这个问题 total_cost = float(input("Cost of dream house")) down_payment = total_cost * 0.25 annual_salary = float(input("Annual salary")) monthly_salary = annual_salary / 12 cur
total_cost = float(input("Cost of dream house"))
down_payment = total_cost * 0.25
annual_salary = float(input("Annual salary"))
monthly_salary = annual_salary / 12
current_savings = 0
x = 100.0000
low = 0.0000
high = 1.0000
guess = (high + low) / 2
r = 0.04
portion_saved = monthly_salary * guess
semi_annual_raise = (100 + 100*(float(input("Semi annual raise")))) / 100
number_of_months = 0.00
max_number_of_months = float(input("Max Time (in months)"))
while abs(current_savings - down_payment) > x:
if current_savings - down_payment > x:
high = guess
guess = (high + low) / 2
portion_saved = monthly_salary * guess
current_savings = 0
elif down_payment - current_savings > x:
low = guess
guess = (high + low) / 2
portion_saved = monthly_salary * guess
current_savings = 0
else:
guess = guess
portion_saved = monthly_salary * guess
if number_of_months < max_number_of_months and number_of_months % 6 == 0:
current_savings *= (100 + r / 12) / 100
portion_saved *= semi_annual_raise
current_savings += portion_saved
number_of_months += 1
elif number_of_months < max_number_of_months and number_of_months % 6 != 0:
current_savings *= (100 + r / 12) / 100
current_savings += portion_saved
number_of_months += 1
print(current_savings)
print(number_of_months)
总成本=浮动(输入(“梦想之家的成本”))
首期付款=总成本*0.25
年薪=浮动(输入(“年薪”))
月薪=年薪/12
当前储蓄=0
x=100.0000
低=0.0000
高=1.0000
猜测=(高+低)/2
r=0.04
保存部分=月工资*猜测
半年加薪=(100+100*(浮动(输入(“半年加薪”))/100
月数=0.00
最大月数=浮动(输入(“最大时间(月)”)
而abs(活期储蓄-首付)>x:
如果当前存款-首付>x:
高=猜测
猜测=(高+低)/2
保存部分=月工资*猜测
当前储蓄=0
elif首付-活期储蓄>x:
低=猜测
猜测=(高+低)/2
保存部分=月工资*猜测
当前储蓄=0
其他:
猜
保存部分=月工资*猜测
如果月数小于月数的最大值且月数%6==0:
当前储蓄*=(100+r/12)/100
保存部分*=半年度加薪
当前存款+=部分存款
月数+=1
elif月数<最大月数和月数%6!=0:
当前储蓄*=(100+r/12)/100
当前存款+=部分存款
月数+=1
打印(活期存款)
打印(月数)
预期结果:第一个if-else语句提供在第二个if-else语句中使用的guess值,如果它abs(当前储蓄-首付)>x,循环将再次运行,直到abs(当前储蓄-首付)“x=当前节省y=部分节省z=月数”
""" x = current_savings y = portion_saved z = number_of_months """
while x < 250000:
if z >= 1 and z % 6 == 0:
x *= (100 + 4 / 12) / 100
y *= 1.07
x += y
z += 1
else:
x *= (100 + 4 / 12) / 100
x += y
z += 1
L1 = [x, z]
return L1
def rate(a, r_rate, lo, hi):
""" a = number_of_months"""
if a - 36 > 0:
lo = r_rate
r_rate = (hi + lo) / 2.0
L2 = [r_rate, lo]
return L2
else:
hi = r_rate
r_rate = (hi + lo) / 2.0
L3 = [r_rate, hi]
return L3
total_cost = 1000000
down_payment = total_cost * 0.25
annual_salary = int(input("Annual Salary"))
monthly_salary = annual_salary / 12
current_savings = 0
number_of_months = 0
low = 0.0
high = 1.0
r = 0.5
num_tries = 0
while abs(current_savings - down_payment) > 100:
portion_saved = monthly_salary * r
current_savings = 0
number_of_months = 0
L5 = [savings(current_savings, portion_saved, number_of_months)[0], savings(current_savings, portion_saved, number_of_months)[1]]
current_savings = L5[0]
number_of_months = L5[1]
L6 = [(rate(number_of_months, r, low, high)[0]), (rate(number_of_months, r, low, high)[1])]
r = L6[0]
if number_of_months - 36 > 0:
low = L6[1]
else:
high = L6[1]
num_tries += 1
print(r)
print(num_tries)
当x<250000时:
如果z>=1且z%6==0:
x*=(100+4/12)/100
y*=1.07
x+=y
z+=1
其他:
x*=(100+4/12)/100
x+=y
z+=1
L1=[x,z]
返回L1
def速率(a、r_速率、lo、hi):
“a=月数”
如果a-36>0:
lo=r_率
利率=(高+低)/2.0
L2=[r_比率,lo]
返回L2
其他:
hi=r_比率
利率=(高+低)/2.0
L3=[r_率,高]
返回L3
总成本=1000000
首期付款=总成本*0.25
年薪=整数(输入(“年薪”))
月薪=年薪/12
当前储蓄=0
月数=0
低=0.0
高=1.0
r=0.5
尝试次数=0
而abs(活期储蓄-首付)>100:
保存部分=月工资*r
当前储蓄=0
月数=0
L5=[储蓄(当前储蓄,部分储蓄,每月数)[0],储蓄(当前储蓄,部分储蓄,每月数)[1]]
当前储蓄=L5[0]
月数=L5[1]
L6=[(比率(月数,r,低,高)[0]),(比率(月数,r,低,高)[1])]
r=L6[0]
如果月数-36>0:
低=L6[1]
其他:
高=L6[1]
尝试次数+=1
印刷品(r)
打印(尝试次数)
“x=当前储蓄y=部分储蓄z=月数”
当x<250000时:
如果z>=1且z%6==0:
x*=(100+4/12)/100
y*=1.07
x+=y
z+=1
其他:
x*=(100+4/12)/100
x+=y
z+=1
L1=[x,z]
返回L1
def速率(a、r_速率、lo、hi):
“a=月数”
如果a-36>0:
lo=r_率
利率=(高+低)/2.0
L2=[r_比率,lo]
返回L2
其他:
hi=r_比率
利率=(高+低)/2.0
L3=[r_率,高]
返回L3
总成本=1000000
首期付款=总成本*0.25
年薪=整数(输入(“年薪”))
月薪=年薪/12
当前储蓄=0
月数=0
低=0.0
高=1.0
r=0.5
尝试次数=0
而abs(活期储蓄-首付)>100:
保存部分=月工资*r
当前储蓄=0
月数=0
L5=[储蓄(当前储蓄,部分储蓄,每月数)[0],储蓄(当前储蓄,部分储蓄,每月数)[1]]
当前储蓄=L5[0]
月数=L5[1]
L6=[(比率(月数,r,低,高)[0]),(比率(月数,r,低,高)[1])]
r=L6[0]
如果月数-36>0:
低=L6[1]
其他:
高=L6[1]
尝试次数+=1
印刷品(r)
打印(尝试次数)
下面是一个指向整个问题集解决方案的链接:
下面是一个指向整个问题集解决方案的链接:
虽然这在理论上可以回答问题,但在此处包含答案的基本部分,并提供链接供参考。虽然这在理论上可以回答问题,但在此处包含答案的基本部分,并提供链接供参考。
# data supplied by the user
base_annual_salary = float(input('Enter your annual salary: '))
# data that is fixed
portion_down_payment = 0.25
rate_of_return = 0.04
monthly_rate_of_return = rate_of_return / 12
total_cost = 1000000
down_payment = total_cost * portion_down_payment
semi_annual_raise = 0.07
months = 36
# initially savings are zero. This variable is the core part of the decrementing
# function used to stop the algorithm
current_savings = 0.0
# there is an acceptable margin of error for this algorithm
epsilon = 100
# define high and low bounds for the bisection search
initial_high = 10000
high = initial_high
low = 0
portion_saved = (high + low) // 2
steps = 0
# use bisection search to find the solution
while abs(current_savings - down_payment) > epsilon:
steps += 1
current_savings = 0.0
annual_salary = base_annual_salary
monthly_salary = annual_salary / 12
monthly_deposit = monthly_salary * (portion_saved / 10000)
for month in range(1, months + 1):
current_savings *= 1 + monthly_rate_of_return
current_savings += monthly_deposit
# problem states that semi-annual raises take effect the next month, so
# mutate monthly_salary after mutating current_savings
if month % 6 == 0:
annual_salary *= 1 + semi_annual_raise
monthly_salary = annual_salary / 12
monthly_deposit = monthly_salary * (portion_saved / 10000)
prev_portion_saved = portion_saved
if current_savings > down_payment:
high = portion_saved
else:
low = portion_saved
# if the solution is outside of the search space on the high bound, low
# will eventually equal the inital high value. However, if we use integer
# division, low will be one less than high. As such, we round the average
# of high and low and cast to an int so that low and high will converge
# completely if the solution is outside of the search space on the high
# bound
portion_saved = int(round((high + low) / 2))
# if portion_saved is no longer changing, our search space is no longer
# changing (because the search value is outside the search space), so we
# break to stop an infinite loop
if prev_portion_saved == portion_saved:
break
if prev_portion_saved == portion_saved and portion_saved == initial_high:
print('It is not possible to pay the down payment in three years.')
else:
print('Best savings rate:', portion_saved / 10000)
print('Steps in bisection search:', steps)