Problem set 1

买房之路:从储蓄到投资
该博客探讨了在高房价地区如何通过明智的投资和储蓄购买梦想家园。它分为三个部分:A部分涉及计算储蓄所需月数,考虑了初始年薪、储蓄比例和房价等因素;B部分加入了每六个月一次的薪资增长,进一步影响储蓄计划;C部分则寻找在特定时间内达成目标储蓄率的方法,使用二分搜索法确定最佳储蓄策略。
Part A: House Hunting
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 (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 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
month, the 18th 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)
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 semi­annual 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 right amount to save away
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 semi­annual 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 search 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. Please makyour
program print results in the format shown in the test cases below.   
Note: There are multiple right ways to implement bisection search/number of steps so your
results may not perfectly mat
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}')

 
【无人机】基于改进粒子群算法的无人机路径规划研究[和遗传算法、粒子群算法进行比较](Matlab代码实现)内容概要:本文围绕基于改进粒子群算法的无人机路径规划展开研究,重点探讨了在复杂环境中利用改进粒子群算法(PSO)实现无人机三维路径规划的方法,并将其与遗传算法(GA)、标准粒子群算法等传统优化算法进行对比分析。研究内容涵盖路径规划的多目标优化、避障策略、航路点约束以及算法收敛性和寻优能力的评估,所有实验均通过Matlab代码实现,提供了完整的仿真验证流程。文章还提到了多种智能优化算法在无人机路径规划中的应用比较,突出了改进PSO在收敛速度和全局寻优方面的优势。; 适合人群:具备一定Matlab编程基础和优化算法知识的研究生、科研人员及从事无人机路径规划、智能优化算法研究的相关技术人员。; 使用场景及目标:①用于无人机在复杂地形或动态环境下的三维路径规划仿真研究;②比较不同智能优化算法(如PSO、GA、蚁群算法、RRT等)在路径规划中的性能差异;③为多目标优化问题提供算法选型和改进思路。; 阅读建议:建议读者结合文中提供的Matlab代码进行实践操作,重点关注算法的参数设置、适应度函数设计及路径约束处理方式,同时可参考文中提到的多种算法对比思路,拓展到其他智能优化算法的研究与改进中。
### 关于CS50P Problem Set 1 的资源与解决方案 对于学习者而言,解决问题的过程通常涉及运行代码、处理数据集并验证输出结果[^1]。然而,在寻找具体课程的解答或资源时,需注意遵循学术诚信原则。 #### 官方文档与教程 CS50P(Python Track)作为哈佛大学推出的入门级编程课程之一,其官方材料提供了详尽的学习指南和练习说明。建议优先查阅该课程官网上的Problem Set描述文件以及相关视频讲解。这些资料不仅涵盖了问题的核心要点,还提供了解决思路提示。 #### 社区支持平台 除了官方渠道外,还可以利用一些在线社区获取帮助。例如Stack Overflow或者Reddit中的r/learnprogramming板块都是不错的讨论场所。不过需要注意提问方式要清晰准确,并展示自己已有的尝试过程以便获得更有针对性的回答。 #### 推荐深入学习路径 如果希望进一步提升计算机视觉领域的能力,则可以考虑参加更高级别的培训项目比如斯坦福大学开设的CS231n课程【卷积神经网络应用于视觉识别】。此课程通过线上发布全部讲课录像连同配套讲义作业等形式为学员们构建了一个全面的知识体系框架;而在此之前如果有兴趣的话也可以先体验一下fast.ai第二部分或者是deeplearning.ai系列内容来打下坚实的基础[^2]。 ```python # 示例:简单的 Python 函数用于计算阶乘 def factorial(n): """Return the factorial of a non-negative integer n.""" if n < 0: raise ValueError("Factorial is not defined for negative numbers.") elif n == 0 or n == 1: return 1 else: result = 1 for i in range(2, n + 1): result *= i return result print(factorial(5)) # Output: 120 ``` 上述代码片段展示了如何定义一个基本函数以实现特定功能——这里是求解整数N的阶乘值。这只是一个非常基础的例子,旨在启发思考关于编写有效程序逻辑的重要性。
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值