I am getting 11 or 12 of 15 correct in a Python course on edX.org each time I submit, but not getting much help from anyone in the discussions because no one can really post any code there (not really helpful) and there doesn't seem to be any available support staff to speak with from the course, which I would pay for, so I am posting here. I was about to pay someone to tutor me but no one is available right now, and I am under some pressure to get this course done by December for my job.
This is the assignment:
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, 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.0
Monthly unpaid balance = (Previous balance) - (Minimum fixed monthly payment)
Updated balance each month = (Monthly unpaid balance) + (Monthly interest rate x Monthly unpaid balance)
This is my code:
#! /usr/bin/python3.6
from math import ceil
def roundup(x):
return int(ceil(x / 10.0) * 10)
def getFixedPayment(balance,annualInterestRate,counter=12):
totalLoan = balance + (balance*annualInterestRate)
monthlyPayment = roundup(totalLoan/12.0)
newBalance = totalLoan - monthlyPayment
if counter < 12:
newPayment = newBalance / counter + 1
else:
newPayment = newBalance / counter
if counter == 1:
return roundup(newPayment/12)
else:
return getFixedPayment(balance,annualInterestRate,counter-1)
#balance = 3329
#annualInterestRate = 0.2
print('Lowest Payment: ' + str(getFixedPayment(balance,annualInterestRate)))
Here are the test results: (I have here all 15, so you might be able to identify a pattern I can't see. The ones marked "ERROR" are the ones I got incorrect)
Test Case 1
balance = 3329; annualInterestRate = 0.2
Output:
Lowest Payment: 310
Test Case 2
balance = 4773; annualInterestRate = 0.2
Output:
Lowest Payment: 440
Test Case 3
balance = 3926; annualInterestRate = 0.2
Output:
Lowest Payment: 360
Randomized Test Case 1
balance = 265; annualInterestRate = 0.18
Output:
Lowest Payment: 30
Randomized Test Case 2
balance = 263; annualInterestRate = 0.18
Output:
Lowest Payment: 30
Randomized Test Case 3
balance = 317; annualInterestRate = 0.25
Output:
Lowest Payment: 30
Randomized Test Case 4
balance = 720; annualInterestRate = 0.2
Output:
Lowest Payment: 70
Randomized Test Case 5
balance = 4284; annualInterestRate = 0.2
Output:
Lowest Payment: 400
Randomized Test Case 6
balance = 3834; annualInterestRate = 0.15
Your output:
Lowest Payment: 340
*** ERROR: Expected Lowest Payment: 350
, but got Lowest Payment: 340
***
Correct output:
Lowest Payment: 350
Randomized Test Case 7
balance = 3045; annualInterestRate = 0.18
Output:
Lowest Payment: 280
Randomized Test Case 8
balance = 4461; annualInterestRate = 0.2
Output:
Lowest Payment: 410
Randomized Test Case 9
balance = 4657; annualInterestRate = 0.04
Your output:
Lowest Payment: 370
*** ERROR: Expected Lowest Payment: 400
, but got Lowest Payment: 370
***
Correct output:
Lowest Payment: 400
Randomized Test Case 10
balance = 3395; annualInterestRate = 0.2
Your output:
Lowest Payment: 320
*** ERROR: Expected Lowest Payment: 310
, but got Lowest Payment: 320
***
Correct output:
Lowest Payment: 310
Randomized Test Case 11
balance = 4045; annualInterestRate = 0.15
Your output:
Lowest Payment: 360
*** ERROR: Expected Lowest Payment: 370
, but got Lowest Payment: 360
***
Correct output:
Lowest Payment: 370
Randomized Test Case 12
balance = 3963; annualInterestRate = 0.18
Output:
Lowest Payment: 360