Minimum Payment for a Credit Card using Binary Search
2. Katas
Problem:
Write a program that considers the Minimum Payment for a Credit Card. Which will pay off the outstanding balance, if you pay it every month for a year.Using Binary Search
Note: Minimum Payment is a fixed amount for all 12 months.
For this, two variables are given to enter the function:
- outStandingBalance - outstanding balance on credit card.
- yearlyPercentRate - yearly percent rate.
Note: If you want to know how to calculate the outstanding balance after one month without a monthly percent rate, See the section below. Example.
Task:
Write a function that returns such a string: "Minimum Payment every month: b | Count used: c", where b is the outstanding balance (rounded to the second decimal point) that the function returned and c is the number of attempts the function used to find the minimum payment.
Example:
To find the outstanding balance after one month, you need to adhere to such an algorithm:
-
Calculate monthly percent rate:
monthlyPercentRate = yearlyPersentRate/12
-
Calculate lower and upper limit:
lowerPay = start_balance/12
upperPay = (start_balance * (1 + monthlyPercentRate)**12)/12.0
-
Calculate the minimum payment we can make in a month:
minPay = (upperPay + lowerPay)/2
-
Calculate the outstanding balance for one action. For an extensive solution, see 2.Katas (Little Python), following the link:
utStandingBalance = outStandingBalance - minPay + ((outStandingBalance - minPay) * monthlyPercentRate)
Note: The main part of the Binary search algorithm, it's up to you.
Data to verify:
creditCardMinPay_BS - function name
- creditCardMinPay_BS(320000,0.2) --> "Minimum Payment every month: 29157.09 | Count used: 17"
- creditCardMinPay_BS(999999,0.18) --> "Minimum Payment every month: 90325.02 | Count used: 22"
- creditCardMinPay_BS(999999,0.18) --> "Minimum Payment every month: 32809.62 | Count used: 20"
where "-->" means after starting the program
Note: For comparison, you can run one of the values using the program that we did in 2.Katas (Median Python). Algorithms is power?
What's next ?
The program does not come out or works not exactly,go to the solution folder and look at the explanation for solving the problem, this will help you.
If you have everything turned out, you can go to the solution package and compare your solution with the one that's there.
If your decision does not coincide with my decision, then I'm very happy for you. Please share it with me, I will be very grateful to you and then I will add it to the solution folder. How to add your solution, see here