1

I've written a solution for an EdX course homework problem using iteration.

The code takes a credit card balance and annual interest rate to calculate the minimum monthly payment required to pay off the balance (plus any interest) in 12 months.

Here is my iterative code:

def minimum_payment_iter(ann_interest, balance):

    month_int = ann_interest/12
    remaining = balance
    payment = 10
    months = 1

    while remaining > 0:

        months = 1
        payment += 10
        remaining = balance

        while months < 13:
            remaining -= payment
            remaining += remaining*month_int
            months += 1


    return payment

I've taken a stab at doing the recursive version, but I've exceeded the maximum recursion depth:

def minimum_payment_recur(ann_interest, balance, payment = 10):

    month_int = ann_interest/12
    remaining = balance
    month = 1


    if remaining <= 0:
        return payment

    else:

        remaining -= payment
        remaining += remaining*month_int
        month += 1

    return minimum_payment_recur(ann_interest, balance, payment + 10)
martineau
  • 119,623
  • 25
  • 170
  • 301
  • don't you have to pass ``remaining`` as your second parameter to the recursive call instead of ``balance``? – Mike Scotty Jul 02 '17 at 19:29
  • Moreover, in the recursive version `month` is always reinitialized to 1. And what is `month_int` ? – Adrien Matissart Jul 02 '17 at 19:32
  • month_int is monthly interest rate, the annual interest rate divided by 12 – Jonathan Ramirez Jul 02 '17 at 19:42
  • Your non-recursive version doesn't work. The result of `minimum_payment_iter(.06, 100.00)` is `20`, which is clearly wrong. I'd get it working first, then worry about writing a recursive version. – martineau Jul 02 '17 at 19:46
  • So as I understand it the "if" condition should be the "terminating" condition, which in this case if the credit card balance "remaining" <= 0. How would I get that to also make sure that the months are incrementing to 12 and resetting every time the minimum payment isn't enough? – Jonathan Ramirez Jul 02 '17 at 19:48

1 Answers1

0

I just did this one. You need to add a counter in the recursion, which is what you would do in a loop.

So, something like:

def function(a,b,counter=12):
    """
    Docstring goes here
    """
    if counter == 1:
        return a+b
    else:
        return function(a,b,counter-1)

I find it tough to get real help in these online courses on edX as well because the courses are so strict on what you can post, and I never got an offer to speak with an individual outside of the "Discussion". I'd pay a premium if I could get 1 on 1 help from edX courses, honestly.

Anyway, hope that helps you.

Debug255
  • 335
  • 3
  • 17