关于利息计算,信用卡最低还款额的python编程问题。

开源好! 发布于 2012/10/15 12:43
阅读 2K+
收藏 1

求助python编程,我最近在跟edx的6.00x课程,虽然有网易的往期视频翻译教程可用,不过好多还是学起来很吃力,现在第一个Pset已经到了最后期限,可是还是弄不出来。一个关于信用卡最低还款额,利息及余额的计算问题。是否有英语编程的python高手,或者也正在学习6.00x课程的同学,给些指导呀,我实在是搞不定了,提问都还搞不太明白,程序都编不出来呀。这个英语授课,虽然有以往中文译文参考,可是学起来也太挑战了。

以下是问题:

第一个问题:

Problem 1: Paying the Minimum : 10.0 points

Write a program to calculate the credit card balance after one year if a person only pays the minimum monthly payment required by the credit card company each month.

The following variables contain values as described below:

    balance - the outstanding balance on the credit card

    annualInterestRate - annual interest rate as a decimal

    monthlyPaymentRate - minimum monthly payment rate as a decimal

For each month, calculate statements on the monthly payment and remaining balance, and print to screen something of the format:


Month: 1
Minimum monthly payment: 96.0
Remaining balance: 4784.0

Be sure to print out no more than two decimal digits of accuracy - so print


Remaining balance: 813.41

instead of


Remaining balance: 813.4141998135

Finally, print out the total amount paid that year and the remaining balance at the end of the year in the format:


Total paid: 96.0
Remaining balance: 4784.0

A summary of the required math is found below:

    Monthly interest rate= (Annual interest rate) / 12
    Minimum monthly payment = (Minimum monthly payment rate) x (Previous balance)
    Updated balance each month = (Previous balance - Minimum monthly payment) x (1 + Monthly interest rate)

Note that the grading script looks for the order in which each value is printed out and the colon separating the description and the number.

The code you paste into the following box should not specify the values for the variables balance, annualInterestRate, or monthlyPaymentRate - our test code will define those values before testing your submission.

我的第一问代码:
month=1
totalpaid=0
for month<=12:
    print 'Month: ' month
    print 'Minimum monthly payment: ' round(balance*monthlyPaymentRate,2)
    totalpaid=totalpaid+(balance-round(balance*monthlyPaymentRate,2)*annualInterestRate/12
    balance=(balance-balance*monthlyPaymentRate)*(1+annualInterestRate/12)
    print round(balance,2)
    month +=1
print 'Total paid: ' round(totalpaid,2)
print 'Remaining balance: ' round(balance,2)

不过都通不过测试,无法运行。第二问都搞不懂具体题意,泪呀

第二问:
 Problem 2: Paying Debt Off In a Year : 15.0 points

Now write a program that calculates the minimum fixed monthly payment needed in order pay off a credit card balance within 12 months. By a fixed monthly payment, we mean a single number which does not change each month, but instead is a constant amount that will be paid each month.

In this problem, we will not be dealing with a minimum monthly payment rate.

The following variables contain values as described below:

    balance - the outstanding balance on the credit card

    annualInterestRate - annual interest rate as a decimal

The program should print out one line: the lowest monthly payment that will pay off all debt in under 1 year.


Lowest Payment: 180

Assume that the interest is compounded monthly according to the balance at the end of the month (after the payment for that month is made). The monthly payment must be a multiple of $10 and is the same for all months. Notice that it is possible for the balance to become negative using this payment scheme, which is okay. A summary of the required math is found below:

    Monthly interest rate = (Annual interest rate) / 12
    Updated balance each month = (Previous balance - Minimum monthly payment) x (1 + Monthly interest rate)

The code you paste into the following box should not specify the values for the variables balance or annualInterestRate - our test code will define those values before testing your submission.


第三问:
 Problem 3: Using Bisection Search to Make the Program Faster : 25.0 points

You'll notice that in problem 2, your monthly payment had to be a multiple of $10. Why did we make it that way? You can try running your code locally so that the payment can be any dollar and cent amount (in other words, the monthly payment is a multiple of $0.01). Does your code still work? It should, but you may notice that your code runs more slowly, especially in cases with very large balances and interest rates. (Note: when your code is running on our servers, there are limits on the amount of computing time each submission is allowed, so your observations from running this experiment on the grading system might be limited to an error message complaining about too much time taken.)

Well then, how can we calculate a more accurate fixed monthly payment than we did in Problem 2 without running into the problem of slow code? We can make this program run faster using a technique introduced in lecture - bisection search!

The following variables contain values as described below:

    balance - the outstanding balance on the credit card

    annualInterestRate - annual interest rate as a decimal

To recap the problem: we are searching for the smallest monthly payment such that we can pay off the entire balance within a year. What is a reasonable lower bound for this payment value? $0 is the obvious anwer, but you can do better than that. If there was no interest, the debt can be paid off by monthly payments of one-twelfth of the original balance, so we must pay at least this much every month. One-twelfth of the original balance is a good lower bound.

What is a good upper bound? Imagine that instead of paying monthly, we paid off the entire balance at the end of the year. What we ultimately pay must be greater than what we would've paid in monthly installments, because the interest was compounded on the balance we didn't pay off each month. So a good upper bound for the monthly payment would be one-twelfth of the balance, after having its interest compounded monthly for an entire year.

In short:

    Monthly interest rate = (Annual interest rate) / 12
    Monthly payment lower bound = Balance / 12
    Monthly payment upper bound = (Balance x (1 + Monthly interest rate)12) / 12

Write a program that uses these bounds and bisection search (for more info check out the Wikipedia page on bisection search) to find the smallest monthly payment to the cent (no more multiples of $10) such that we can pay off the debt within a year. Try it out with large inputs, and notice how fast it is (try the same large inputs in your solution to Problem 2 to compare!). Produce the same return value as you did in problem 2.

Note that if you do not use bisection search, your code will not run - your code only has 30 seconds to run on our servers. If you get a message that states "Your submission could not be graded. Please recheck your submission and try again. If the problem persists, please notify the course staff.", check to be sure your code doesn't take too long to run.

The code you paste into the following box should not specify the values for the variables balance or annualInterestRate - our test code will define those values before testing your submission.


以下是问题补充:

@开源好!:我知道,大家都是为我的学习好。可是可否能解释一下第二个和第三个问题?就是我现在连第二个和第三个的问题是什么都没搞懂,第一个还能看明白,后两个完全看不懂是什么问题呀!!! (2012/10/15 13:31)
加载中
0
晴风晓月
晴风晓月

第二个问题的大意是:编程找出要在12个月内付完所有欠款的每月最低还款额,每月最低还款额必须相同且是10的倍数;

说白了就是分期付款里的等额还款相同,只是增加了一个要是10的倍数

第三题是要求对第二题优化,提高速度

我也只是看了个大概,你自己还需要细看

开源好!
开源好!
我已经完全搞乱了。我发现我数学太糟糕了。连手算测试例子都算不出来了。
开源好!
开源好!
非常感谢您的耐心回复!我自己搞了半天,看来这个题现在是解不出来了。几个变量搞在一起,完全搞乱了。我觉得我的问题出在数学上了,我试了一下计算出例子的数学表示,结果发现我都没算明白,用伪代码都没搞明白怎么算。这回问题大条了。
0
晴风晓月
晴风晓月
最好还是自己认真学吧,貌似这里的人基本不会只帮你做题的。有挑战或许是好事
0
开源好!
开源好!

引用来自“fangjun105”的答案

最好还是自己认真学吧,貌似这里的人基本不会只帮你做题的。有挑战或许是好事
感谢回答!看来这次又完不成了,英语是多么重要啊,否则连题都没怎么看懂
返回顶部
顶部