6.00: Introduction to Computer Science and Programming

Problem Set 1

Handed out: Tuesday, September 15, 2009.
Due: 11:59pm, Tuesday, September 22, 2009.

Introduction

This problem set will introduce you to using control flow in Python and formulating a computational solution to a problem. You will design and write 2 simple Python programs, test them, and hand them in. Be sure to read this problem set thoroughly, especially the sections of Collaboration and the Handin Procedure.

Collaboration

Please take note of the policy in the course information handout.

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 average balance due. However, the credit card company will make revenue by charging interest on the balance that you don’t pay. Hence, even if you pay credit card payments on time, interest is still accruing on the outstanding balance. As a result, the average American family has up to $10,000 in credit card debt (http://moneycentral.msn.com/content/Banking/creditcardsmarts/P117014.asp).

Example: You’ve made a $5,000 purchase on a credit card with 18% annual interest rate and 2% minimum monthly payment rate. After a year, how much is the remaining balance? Use the following equations.

Minimum monthly payment = Minimum monthly payment rate * Balance

(Minimum monthly payment gets split into interest paid and principal paid)

Interest Paid = Annual Interest Rate / 12 months * Balance

Principal Paid = Minimum monthly payment – Interest Paid

Remaining Balance = Balance – Principal Paid

 

For month 1, compute the minimum monthly payment by taking 2% of the balance.

Minimum monthly payment = .02 x $5000 = $100

We can’t simply deduct this from the balance because there is compounding interest. Of this $100 monthly payment, compute how much will go to paying off interest and how much will go to paying off the principal. Remember that the annual interest rate is given, so we need to divide by 12 to get the monthly interest rate. 

Interest paid = .18/12 x $5000 = $75

Principal paid = $100 - $75 = $25

The remaining balance at the end of the first month will be the principal paid this month subtracted from the balance at the start of the month.

Remaining Balance = $5000 - $25 = $4975

 

For month 2, we repeat the same steps.

Minimum monthly payment = .02 x $4975= $99.50

Interest Paid = .18/12 x $4975 = $74.63

Principal Paid = $99.50- $74.63= $24.87

Remaining Balance = $4975 - $24.87 = $4950.13

After 12 months, the total amount paid is $1167.55, leaving an outstanding balance of $4708.10. (Pretty depressing!)

Problem 1. Paying the Minimum

Write a program to calculate the credit card balance after 1 year if a person only pays the minimum monthly payment required by the credit card company each month (i.e. 2% of their current balance).

Take as input (1) the outstanding balance on the credit card, (2) annual interest rate as decimal, and (3) minimum monthly payment rate as decimal. For each month, print out statements on the monthly payment and remaining balance as shown in the test cases below. The result should include total amount paid that year and remaining balance.

Hints:

Use the round function in Python. (Look up this function online).

To help you get started, here is a rough outline of the stages you should probably follow in writing your code:

Retrieve user input

Initialize some state variables (remember to find monthly interest rate from the annual interest rate taken in as input)

For each month, compute the new balance

This requires computing the monthly minimum payment and figuring out how much will be paid to interest and how much will be paid to the principal

Update the outstanding balance according to how much principal was paid off  

Keep track of the total amount of paid over all the past months so far

Stop when you reach some appropriate end condition.  Print out the result statement (total amt paid, remaining balance)

Use these ideas to guide the creation of your code.

Test Cases:

In recent years, many credit card corporations tightened restrictions by raising their minimum monthly payment rate to 4%. As illustrated in the second test case below, people will be able to pay less interest over the years and get out of debt faster.

Test Case 1:

Enter the outstanding balance on your credit card: 4800

Enter the annual credit card interest rate as a decimal: .2

Enter the minimum monthly payment rate as a decimal: .02

Month: 1

Minimum monthly payment: 96.0

Remaining balance: 4784.0

Month: 2

Minimum monthly payment: 95.68

Remaining balance: 4768.05

Month: 3

Minimum monthly payment: 95.36

Remaining balance: 4752.16

Month: 4

Minimum monthly payment: 95.04

Remaining balance: 4736.32

Month: 5

Minimum monthly payment: 94.73

Remaining balance: 4720.53

Month: 6

Minimum monthly payment: 94.41

Remaining balance: 4704.8

Month: 7

Minimum monthly payment: 94.1

Remaining balance: 4689.11

Month: 8

Minimum monthly payment: 93.78

Remaining balance: 4673.48

Month: 9

Minimum monthly payment: 93.47

Remaining balance: 4657.9

Month: 10

Minimum monthly payment: 93.16

Remaining balance: 4642.37

Month: 11

Minimum monthly payment: 92.85

Remaining balance: 4626.89

Month: 12

Minimum monthly payment: 92.54

Remaining balance: 4611.46

RESULT

Total amount paid: 1131.12

Remaining balance: 4611.46

 

Test Case 2:

Enter the outstanding balance on your credit card: 4800

Enter the annual credit card interest rate as a decimal: .2

Enter the minimum monthly payment rate as a decimal: .04

Month: 1

Minimum monthly payment: 192.0

Remaining balance: 4688.0

Month: 2

Minimum monthly payment: 187.52

Remaining balance: 4578.61

Month: 3

Minimum monthly payment: 183.14

Remaining balance: 4471.78

Month: 4

Minimum monthly payment: 178.87

Remaining balance: 4367.44

Month: 5

Minimum monthly payment: 174.7

Remaining balance: 4265.53

Month: 6

Minimum monthly payment: 170.62

Remaining balance: 4166.0

Month: 7

Minimum monthly payment: 166.64

Remaining balance: 4068.79

Month: 8

Minimum monthly payment: 162.75

Remaining balance: 3973.85

Month: 9

Minimum monthly payment: 158.95

Remaining balance: 3881.13

Month: 10

Minimum monthly payment: 155.25

Remaining balance: 3790.57

Month: 11

Minimum monthly payment: 151.62

Remaining balance: 3702.13

Month: 12

Minimum monthly payment: 148.09

Remaining balance: 3615.74

RESULT

Total amount paid: 2030.15

Remaining balance: 3615.74

If you want to check that your code correctly for more examples, create your own test cases with this online credit card calculator: http://www.bankrate.com/calculators/managing-debt/minimum-payment-calculator.aspx Compare the first 12 rows of the resulting table with the output of your program.

Problem 2. Paying Debt Off In a Year

Now write a program that calculates the minimum fixed monthly payment in order pay off a credit card balance within 12 months. Since the monthly payment is more than what is required by the credit card company, we don’t need to deal with the minimum monthly payment rate in this problem.

Take as input (1) an initial outstanding balance and (2) annual interest rate. For the result, print out the fixed minimum monthly payment, number of months (at most 12) it takes to pay off the debt, and the balance (likely to be a negative number). 

Assume that the interest is compounded monthly according to the balance at the start of the month (before the payment for that month is made). The monthly payment must be a multiple of $100 and is the same for all months. Notice that it is possible for the balance to become negative using this payment scheme.   

Hints:

Start at $100 payments per month and simulate whether the balance will be paid off (taking into account the interest accrued each month). If $100 monthly payments are insufficient to pay off the debt in 1 year, repeat with a higher monthly payment.

Test Case 1:

Enter the outstanding balance on your credit card: 1200

Enter the annual credit card interest rate as a decimal: .18

RESULT

Monthly payment to pay off debt in 1 year: 200

Number of months needed: 7

Balance: -132.78

Test Case 2:

Enter the outstanding balance on your credit card: 32000

Enter the annual credit card interest rate as a decimal: .2

RESULT

Monthly payment to pay off debt in 1 year: 3000

Number of months needed: 12

Balance: -469.88

Test Case 3:

Enter the outstanding balance on your credit card: 67242

Enter the annual credit card interest rate as a decimal: .19

RESULT

Monthly payment to pay off debt in 1 year: 6200

Number of months needed: 12

Balance: -42.02

Hand-In Procedure

1. Save

Save your solution to Problem 1 as ps1a.py and your solution to Problem 2 as ps1b.py. Do not ignore this step or save your file(s) with different names.

2. Time and Collaboration Info

At the start of each file, in a comment, write down the number of hours (roughly) you spent on the problems in that part, and the names of the people you collaborated with. For example:

 # Problem Set 1

 # Name: Jane Lee

 # Collaborators (Discussion): John Doe

 # Collaborators (Identical Solution): Jane Smith

 # Time: 1:30

 # 

... your code goes here ... 

 

3. Submit

To submit a file, upload it to your workspace. If there is some error uploading to your workspace, email the file to 6.00-staff [at] mit.edu.

You may upload (or email) new versions of each file until the 11:59pm deadline.