亚洲香蕉成人av网站在线观看_欧美精品成人91久久久久久久_久久久久久久久久久亚洲_热久久视久久精品18亚洲精品_国产精自产拍久久久久久_亚洲色图国产精品_91精品国产网站_中文字幕欧美日韩精品_国产精品久久久久久亚洲调教_国产精品久久一区_性夜试看影院91社区_97在线观看视频国产_68精品久久久久久欧美_欧美精品在线观看_国产精品一区二区久久精品_欧美老女人bb

首頁 > 編程 > Python > 正文

MIT 6.00.1x 計算機科學和Python編程導論 Set 2

2019-11-06 08:13:47
字體:
來源:轉載
供稿:網友

Paying Off Credit Card Debt

Each month, a credit card statement will come with the option for you to pay a minimum amount of your charge, usually 2% of the balance due. However, the credit card company earns money by charging interest on the balance that you don’t pay. So even if you pay credit card payments on time, interest is still accruing on the outstanding balance.

You can think about this in the following way.

At the beginning of month 0 (when the credit card statement arrives), assume you owe an amount we will call b0 (b for balance; subscript 0 to indicate this is the balance at month 0).

Any payment you make during that month is deducted from the balance. Let’s call the payment you make in month 0, p0. Thus, your unpaid balance for month 0, ub0, is equal to b0?p0. ub0=b0?p0

At the beginning of month 1, the credit card company will charge you interest on your unpaid balance. So if your annual interest rate is r, then at the beginning of month 1, your new balance is your PRevious unpaid balance ub0, plus the interest on this unpaid balance for the month. In algebra, this new balance would be b1=ub0+r12.0?ub0

In month 1, we will make another payment, p1 . That payment has to cover some of the interest costs, so it does not completely go towards paying off the original charge. The balance at the beginning of month 2, b2, can be calculated by first calculating the unpaid balance after paying p1, then by adding the interest accrued: ub1=b1?p1 b2=ub1+r12.0?ub1

If you choose just to pay off the minimum monthly payment each month, you will see that the compound interest will dramatically reduce your ability to lower your debt.

Let’s look at an example. If you’ve got a $5,000 balance on a credit card with 18% annual interest rate, and the minimum monthly payment is 2% of the current balance, we would have the following repayment schedule if you only pay the minimum payment each month:

Month Balance Minimum Payment Unpaid Balance Interest
0 5000.00 100(=5000* 0.02) 4900(=5000- 100) 73.50(=0.18/12.0 * 4900)
1 4973.50(=4900 + 73.50) 99.47(=4973.50 * 0.02) 4874.03(=4973.50 - 99.47) 73.11(=0.18/12.0 * 4874.03)
2 4947.14(=4874.03 + 73.11) 98.94(=4947.14 * 0.02) 4848.20(=4947.14 - 98.94) 72.72(=0.18/12.0 * 4848.20)

You can see that a lot of your payment is going to cover interest, and if you work this through month 12, you will see that after a year, you will have paid $1165.63 and yet you will still owe $4691.11 on what was originally a $5000.00 debt. Pretty depressing!


Problem 1: Paying the Minimum

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 cardnualInterestRate - annual interest rate as a decimalmonthlyPaymentRate - 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: 1Minimum monthly payment: 96.0Remaining 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.0Remaining balance: 4784.0

A summary of the required math is found below:

Monthly interest rate= (Annual interest rate) / 12.0Minimum monthly payment = (Minimum monthly payment rate) x (Previous balance)Monthly unpaid balance = (Previous balance) - (Minimum monthly payment)Updated balance each month = (Monthly unpaid balance) + (Monthly interest rate x Monthly unpaid balance)monBalance=balancetotalPay=0for month in range(1,13): minPayment=monBalance * monthlyPaymentRate restBalance=monBalance - minPayment interest=restBalance * (annualInterestRate/12.0) monBalance =restBalance + interest totalPay+=minPayment print 'Month:'+str(month) print 'Minimum monthly payment:'+'%.2f' % minPayment #保留兩位小數 print 'Remaining balance:'+'%.2f' % monBalanceprint 'Total paid:'+'%.2f' % totalPayprint 'Remaining balance:'+'%.2f' % monBalance

Problem 2: Paying Debt Off In a Year

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 cardannualInterestRate - 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, for example: 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.0Monthly unpaid balance = (Previous balance) - (Minimum monthly payment)Updated balance each month = (Monthly unpaid balance) + (Monthly interest rate x Monthly unpaid balance)pay=10while True: unpaidBalance=balance for month in range(12): #重復12次 unpaidBalance-=pay unpaidBalance=unpaidBalance + unpaidBalance * (annualInterestRate /12.0) if unpaidBalance <=0: #小于或等于零時退出循環 print 'Lowest Payment: '+str(pay) break else: pay+=10

Problem 3: Using Bisection Search to Make the Program Faster

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 cardannualInterestRate - 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.0Monthly payment lower bound = Balance / 12Monthly payment upper bound = (Balance x (1 + Monthly interest rate)12) / 12.0

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.

lowerBound = balance / 12upperBound = (balance * (1 + annualInterestRate / 12.0) ** 12) / 12.0 while True: pay = (lowerBound + upperBound) / 2.0 unpaidBalance=balance for month in range(12): unpaidBalance-=pay unpaidBalance=unpaidBalance + unpaidBalance * (annualInterestRate /12.0) if abs(unpaidBalance - 0) <=0.01: #控制精度 print 'Lowest Payment: '+'%.2f' % pay break else: if unpaidBalance > 0: lowerBound = pay else: upperBound = pay

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
亚洲香蕉成人av网站在线观看_欧美精品成人91久久久久久久_久久久久久久久久久亚洲_热久久视久久精品18亚洲精品_国产精自产拍久久久久久_亚洲色图国产精品_91精品国产网站_中文字幕欧美日韩精品_国产精品久久久久久亚洲调教_国产精品久久一区_性夜试看影院91社区_97在线观看视频国产_68精品久久久久久欧美_欧美精品在线观看_国产精品一区二区久久精品_欧美老女人bb
国产精品白嫩美女在线观看| 成人午夜激情网| 一区二区欧美日韩视频| 国产视频欧美视频| 国产一区二区香蕉| 福利视频一区二区| 日韩免费不卡av| 欧美激情性做爰免费视频| 成人看片人aa| 国产精品高潮在线| 国产精品视频中文字幕91| 成人写真福利网| 91精品视频一区| 日韩综合中文字幕| 日韩国产激情在线| 中文字幕一精品亚洲无线一区| 亚洲一区二区三区乱码aⅴ| 97在线看福利| 日韩极品精品视频免费观看| 亚洲国产精品中文| 色综合伊人色综合网站| 成人黄色短视频在线观看| 欧美大胆a视频| 国产精品永久免费| 久久久久久97| 国产欧美一区二区三区四区| 日韩av三级在线观看| 66m—66摸成人免费视频| 国产精品99久久99久久久二8| 日韩小视频网址| 精品美女永久免费视频| 欧美片一区二区三区| 麻豆一区二区在线观看| 久久久久久久久国产| 国产精品久久久久久久久借妻| 免费av一区二区| 欧美性生活大片免费观看网址| 国产精品视频中文字幕91| 最近2019中文字幕大全第二页| 成人网在线观看| 最近中文字幕mv在线一区二区三区四区| 伊人一区二区三区久久精品| 久久综合久久88| 日本精品一区二区三区在线| 亚洲人成免费电影| 国产精品一二三视频| 欧美福利视频在线观看| 国产精品成人一区二区三区吃奶| 欧美高清电影在线看| 欧美激情视频给我| 亚洲free性xxxx护士白浆| 日韩高清人体午夜| 欧美一区二区三区免费观看| 97国产精品视频人人做人人爱| 亚洲91精品在线| 亚洲色图欧美制服丝袜另类第一页| 97在线观看免费| 久久久伊人欧美| 成人在线一区二区| 狠狠做深爱婷婷久久综合一区| 午夜精品国产精品大乳美女| 久久久久久久国产精品| 国产亚洲成av人片在线观看桃| 欧美一区在线直播| 日韩中文字幕网| 在线视频国产日韩| 国产精品爽爽ⅴa在线观看| 亚洲自拍偷拍视频| 欧美伦理91i| 人妖精品videosex性欧美| 成人羞羞国产免费| 91tv亚洲精品香蕉国产一区7ujn| 欧美一区第一页| 久久久国产一区| 亚洲成色777777在线观看影院| 在线视频一区二区| 奇米四色中文综合久久| 欧美极品在线视频| 欧美情侣性视频| 8090理伦午夜在线电影| 91国内精品久久| 全色精品综合影院| 中文字幕精品—区二区| 91天堂在线视频| 国产精品视频自拍| 久久精品人人爽| 久久久久久久影院| 欧美人在线视频| 亚洲日本中文字幕免费在线不卡| 亚洲国产成人精品久久久国产成人一区| 久久男人的天堂| 亚洲精品综合久久中文字幕| 在线观看日韩专区| 久久久久久久久久亚洲| 亚洲一区二区三区视频| 国产精品视频一区二区高潮| 欧美日韩高清在线观看| 日韩国产高清视频在线| 在线观看国产精品91| 国产精品丝袜白浆摸在线| 668精品在线视频| 久久久国产精彩视频美女艺术照福利| 国产精品极品尤物在线观看| 亚洲精品av在线| 欧美日韩免费观看中文| 欧美成人免费大片| 美女久久久久久久| 亚洲成人网久久久| 日韩欧美亚洲范冰冰与中字| 81精品国产乱码久久久久久| 日本精品一区二区三区在线播放视频| 国产精品9999| 国产精品久久久久久久久久东京| 国产精品免费网站| 在线观看欧美成人| 久久久精品久久| 欧美激情精品久久久久久大尺度| 91性高湖久久久久久久久_久久99| 国产精品一区av| 国产精品麻豆va在线播放| 色多多国产成人永久免费网站| 美日韩在线视频| 中文字幕日韩av电影| 国产精品444| 中文字幕日韩欧美在线视频| 欧美丰满片xxx777| 另类色图亚洲色图| 国产视频精品xxxx| 欧美日韩亚洲一区二区三区| 92国产精品久久久久首页| 国产精品久久中文| 久久久亚洲影院| 欧美性xxxxxxx| 国产精品亚洲片夜色在线| 日韩av在线免费观看一区| 日韩av一区二区在线观看| 红桃av永久久久| 国产精品久久久久久久电影| 91探花福利精品国产自产在线| 成人亚洲激情网| 久久视频国产精品免费视频在线| 久久福利网址导航| 欧美激情性做爰免费视频| 91精品国产91久久久久久不卡| 精品中文字幕在线2019| 欧美日本啪啪无遮挡网站| 精品亚洲国产视频| 亚洲精品免费在线视频| 国产亚洲精品91在线| 欧美三级欧美成人高清www| 91免费高清视频| 最新国产精品亚洲| 欧美黑人xxxⅹ高潮交| 69国产精品成人在线播放| 成人日韩在线电影| 久久国产精品亚洲| 亚洲欧美日韩区| 日韩精品中文字幕久久臀| 最近的2019中文字幕免费一页| 日韩精品在线播放| 91精品国产乱码久久久久久久久| 精品久久久久久亚洲国产300| 日韩美女在线观看| 亚洲高清福利视频|