We are searching for the smallest monthly payment such that we can pay off the entire balance of a loan within a year.

The following values might be useful when writing your solution

Monthly interest rate = (Annual interest rate) / 12
Monthly payment lower bound = Balance / 12
Monthly payment upper bound = (Balance x (1 + Monthly interest rate)12) / 12

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

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 such that we can pay off the debt within a year.

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. If you get a message that states "Your submission could not be graded. Please recheck your submission and try again. If the problem persists, please notify the course staff.", check to be sure your code doesn't take too long to run.

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.

monthlyInterestRate = annualInterestRate/12 lowerBound = balance/12 upperBound = (balance * (1+annualInterestRate/12)**12)/12 originalBalance = balance # Keep testing new payment values # until the balance is +/- $0.02 while abs(balance) > .02: # Reset the value of balance to its original value balance = originalBalance # Calculate a new monthly payment value from the bounds payment = (upperBound - lowerBound)/2 + lowerBound # Test if this payment value is sufficient to pay off the # entire balance in 12 months for month in range(12): balance -= payment balance *= 1+monthlyInterestRate # Reset bounds based on the final value of balance if balance > 0: # If the balance is too big, need higher payment # so we increase the lower bound lowerBound = payment else: # If the balance is too small, we need a lower # payment, so we decrease the upper bound upperBound = payment # When the while loop terminates, we know we have # our answer! print("Lowest Payment:", round(payment, 2)) {"grader": "ps02/bisect/grade_bisect.py"}
Note:

Depending on where, and how frequently, you round during this function, your answers may be off a few cents in either direction. Try rounding as few times as possible in order to increase the accuracy of your result.

Hints

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!

Note: The automated tests are lenient - if your answers are off by a few cents in either direction, your code is OK.

Test Cases:

              
      Test Case 1:
      balance = 320000
      annualInterestRate = 0.2

      Result Your Code Should Generate:
      -------------------
      Lowest Payment: 29157.09

            

              
      Test Case 2:
      balance = 999999
      annualInterestRate = 0.18

      Result Your Code Should Generate:
      -------------------
      Lowest Payment: 90325.07

            

The autograder says, "Your submission could not be graded." Help!

If the autograder gives you the following message:

Your submission could not be graded.
Please recheck your submission and try again. If the problem persists, please notify the course staff.
      

Don't panic! There are a few things that might be wrong with your code that you should check out. The number one reason this message appears is because your code timed out. You only get 30 seconds of computation time on our servers. If your code times out, you probably have an infinite loop. What to do?

The number 1 thing to do is that you need to run this code in your own local environment. Your code should print one line at the end of the loop. If your code never prints anything out - you have an infinite loop!

To debug your infinite loop - check your loop conditional. When will it stop? Try inserting print statements inside your loop that prints out information (like variables) - are you incrementing or decrementing your loop counter correctly? Search the forum for people with similar issues. If your search turns up nothing, make a new post and paste in your loop conditional for others to help you out with.

Please don't email the course staff unless your code legitimately works and prints out the correct answers in your local environment. In that case, please email your code file, a screenshot of the code printing out the correct answers in your local environment, and a screenshot of the exact same code not working on the tutor. The course staff is otherwise unable to help debug your problem set via email - we can only address platform issues.