Académique Documents
Professionnel Documents
Culture Documents
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.
Say you've made a $5,000 purchase on a credit card with an 18% annual
interest rate and a 2% minimum monthly payment rate. If you only pay the
minimum monthly amount for a year, how much is the remaining 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,
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+(r/12.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
adding the interest accrued:
ub1=b1p1
b2=ub1+(r/12.0)ub1
p1, then by
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
5000.00
Minimum
Payment
Unpaid Balance
Interest
100 (= 5000 *
4900 (= 5000 -
73.50 (=
0.02)
100)
0.18/12.0 * 4900)
4973.50 (= 4900
99.47 (=
4874.03 (=
73.11 (=
+ 73.50)
4973.50 * 0.02)
4973.50 - 99.47)
0.18/12.0 *
4874.03)
4947.14 (=
98.94 (=
4848.20 (=
72.72 (=
4874.03 + 73.11)
4947.14 * 0.02)
4947.14 - 98.94)
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!
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:
1. balance - the outstanding balance on the credit card
2. annualInterestRate - annual interest rate as a decimal
3. 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
Note: Depending on where you round in this problem, your answers may be
off by a few cents in either direction. Do not worry if your solution is within
+/- 0.05 of the correct answer.
Be sure to test these on your own machine - and that you get the same
output! - before running your code on this webpage!
Test Cases:
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
Test Case 1:
balance = 4213
annualInterestRate = 0.2
monthlyPaymentRate = 0.04
Result Your Code Should Generate:
------------------Month: 1
Minimum monthly payment: 168.52
Remaining balance: 4111.89
Month: 2
Minimum monthly payment: 164.48
Remaining balance: 4013.2
Month: 3
Minimum monthly payment: 160.53
Remaining balance: 3916.89
18.
19.
20.
21.
22.
23.
24.
25.
26.
27.
28.
29.
30.
31.
32.
33.
34.
35.
36.
37.
38.
39.
40.
41.
42.
43.
44.
45.
46.
Month: 4
Minimum monthly payment: 156.68
Remaining balance: 3822.88
Month: 5
Minimum monthly payment: 152.92
Remaining balance: 3731.13
Month: 6
Minimum monthly payment: 149.25
Remaining balance: 3641.58
Month: 7
Minimum monthly payment: 145.66
Remaining balance: 3554.19
Month: 8
Minimum monthly payment: 142.17
Remaining balance: 3468.89
Month: 9
Minimum monthly payment: 138.76
Remaining balance: 3385.63
Month: 10
Minimum monthly payment: 135.43
Remaining balance: 3304.38
Month: 11
Minimum monthly payment: 132.18
Remaining balance: 3225.07
Month: 12
Minimum monthly payment: 129.0
Remaining balance: 3147.67
Total paid: 1775.55
Remaining balance: 3147.67
47.
48.
49.
50.
51.
52.
53.
54.
55.
56.
57.
58.
59.
60.
61.
62.
63.
64.
65.
66.
67.
68.
69.
70.
71.
72.
Test Case 2:
balance = 4842
annualInterestRate = 0.2
monthlyPaymentRate = 0.04
Result Your Code Should Generate:
------------------Month: 1
Minimum monthly payment: 193.68
Remaining balance: 4725.79
Month: 2
Minimum monthly payment: 189.03
Remaining balance: 4612.37
Month: 3
Minimum monthly payment: 184.49
Remaining balance: 4501.68
Month: 4
Minimum monthly payment: 180.07
Remaining balance: 4393.64
Month: 5
Minimum monthly payment: 175.75
Remaining balance: 4288.19
Month: 6
73.
74.
75.
76.
77.
78.
79.
80.
81.
82.
83.
84.
85.
86.
87.
88.
89.
90.
91.
92.
93.
94.
95.
96.
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.
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:
Be sure to test these on your own machine - and that you get the same
output! - before running your code on this webpage!
Test Cases:
1.
2.
3.
4.
5.
6.
7.
8.
9.
Test Case 1:
balance = 3329
annualInterestRate = 0.2
Result Your Code Should Generate:
------------------Lowest Payment: 310
10.
11.
12.
13.
14.
15.
16.
17.
18.
Test Case 2:
balance = 4773
annualInterestRate = 0.2
Result Your Code Should Generate:
------------------Lowest Payment: 440
19.
20.
21.
22.
23.
24.
25.
26.
27.
28.
Test Case 3:
balance = 3926
annualInterestRate = 0.2
Result Your Code Should Generate:
------------------Lowest Payment: 360
29.
30.
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.
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.
Test Cases to Test Your Code With. Be sure to test these on your own
machine - and that you get the same output! - before running your
code on this webpage!
Click to See Problem 3 Test Cases
Note: The automated tests are leinient - if your answers are off by a few
cents in either direction, your code is OK.
Be sure to test these on your own machine - and that you get the same
output! - before running your code on this webpage!
Test Cases:
1.
2.
3.
4.
5.
6.
7.
8.
Test Case 1:
balance = 320000
annualInterestRate = 0.2
Result Your Code Should Generate:
------------------Lowest Payment: 29157.09
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
Test Case 2:
balance = 999999
annualInterestRate = 0.18
Result Your Code Should Generate:
------------------Lowest Payment: 90325.03
19.
Problem set 3
RADIATION EXPOSURE
Now here's the problem we'd like to solve. Let's say Sarina has moved into a
new apartment. Unbeknownst to her, there is a sample of Cobalt-60 inside
one of the walls of the apartment. Initially that sample had 10 MBq of
activity, but she moves in after the sample has been there for 5 years. She
lives in the apartment for 6 years, then leaves. How much radiation was she
exposed to?
We can actually figure this out using the radioactive decay curve from above.
What we want to know is hertotal radiation exposure from year 5 to year 11.
Total radiation exposure corresponds to the area between the two green
lines at time = 5 and time = 11, and under the blue radioactive decay curve.
This should make intuitive sense - if the x axis measures time, and the y
axis measures activity, then the area under the curve measures (time *
activity) = MBq*years, or, approximately the total number of MBq Sarina
was exposed to in her time in the radioactive apartment (technically, this
result is the combination of gamma rays and beta particles she was exposed
to, but this gets a bit complicated, so we'll ignore it. Sorry, physicists!).
So far, so good. But, how do we calculate this? Unlike a simple shape - say a
square, or a circle - we have no easy way to tell what the area under this
curve is.
However, we have learned a technique that can help us here
- approximation. Let's use an approximation algorithm to estimate the area
under this curve! We'll do so by first splitting up the area into equally-sized
rectangles (in this case, six of them, one rectangle per year):
Once we've done that, we can figure out the area of each rectangle pretty
easily. Recall that the area of a rectangle is found by multiplying the height
of the rectangle by its width. The height of this rectangle:
The width of the rectangle is 1.0. So the area of this single rectangle
is 1.0*5.181 = 5.181 . To approximate how much radiation Sarina was
exposed to, we next calculate the area of each successive rectangle and then
sum up the areas of each rectangle to get the total. When we do this, we
find that Sarina was exposed to nearly 23 MBq of radiation (technically, her
apartment was bombarded by 23e6 * 3.154e6 = 7.25e13 neutrons, for
those interested...).
Whether or not this will kill Sarina depends exactly on the type of radiation
she was exposed to (see this linkwhich discusses more about the ways of
measuring radiation). Either way, she should probably ask her landlord for a
substantial refund.
In this problem, you are asked to find the amount of radiation a person is
exposed to during some period of time by completing the following function:
To complete this function you'll need to know what the value of the
radioactive decay curve is at various points. There is a function f that will be
defined for you that you can call from within your function that describes the
radioactive decay curve for the problem. Do not define f in your code.
You should implement this function on your own machine. Open a new
Canopy Python file and title it "radiationExposure.py". Complete your work
inside this file. Test your code well in Canopy, and when you are convinced it
is correct, cut and paste your definition into this tutor window.
Test Cases to Test Your Code With. Be sure to test these on your own
machine - and that you get the same output! - before running your
code on this webpage!
Click to See Test Cases
Test case 1:
>>> radiationExposure(0, 5, 1)
39.10318784326239
Test case 2:
Test case 3:
>>> radiationExposure(0, 11, 1)
62.0455982538
Test case 4:
>>> radiationExposure(40, 100, 1.5)
0.434612356115
A note on these test cases: Your answers should be within 0.01 of the
correct answer.
A Mathematical Note of Interest
The technique of finding the area under a curve is called integration. This
comes to us from calculus. What we're doing in this problem is an
approximation of finding the integral under the curve using the summation
of rectangular areas known as a Riemann integral.
This approximation becomes more and more correct the smaller the width of
the rectangles becomes.
So there you have it. If you've not learned calculus before, you've now got
one of the basics - integration - covered!
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.