Python: using recursion to find ten times the minimum fixed monthly payment of 10

I get 11 or 12 of the 15 correct ones in a Python course on edX.org every time I post, but don't get much help from anyone in the discussions, because no one can really post any code there (not very useful ) and it seems that there is no support staff available to talk with the course for which I would pay, so I post here. I was going to pay someone to educate me, but now no one is available, and I am under some pressure to complete this course by December for my work.

This assignment:

Now write a program that calculates the minimum fixed monthly payment required to pay off a credit card balance within 12 months. By a fixed monthly payment, we mean a single number that does not change every month, but instead is a constant amount that will be paid every month.

In this problem we will not deal with the minimum monthly payment.

The following variables contain the values ​​described below:

balance - outstanding credit card balance

annualInterestRate - annual interest rate as decimal

The program should print one line: the lowest monthly payment, which will pay off all debt in less than 1 year, for example:

: 180
, ( ). 10 . , , . :

= ( )/12,0
= ( ) - ( )
= ​​( ) + ( x )

:

#! /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)))

: ( 15, , . , "ERROR", , )

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
+4
2

( ), , .

, counter, 1:

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)
        #                                                 ^^^^^^^^^
        #                                                 the only change!

counter > 1, " ", , counter - 1. , counter = 12, 11 . :

def getFixedPayment(balance, annualInterestRate):
    totalLoan = balance + (balance*annualInterestRate)
    monthlyPayment = roundup(totalLoan/12.0)
    newBalance = totalLoan - monthlyPayment

    newPayment = newBalance / counter + 1

    return roundup(newPayment/12)

? .

, . - 3 , , :

  • T ,
  • r , 4%, 1.04, , T * r.
  • x

, x , ?

(T - x) * r

x , T - x, .

x, :

((T - x) * r - x) * r

x.

- x, :

((T - x) * r - x) * r - x <= 0

, x:

((T - x) * r - x) * r <= x

(T - x) * r - x <= x / r

(T - x) * r <= x / r + x

T - x <= x / r / r + x / r

T <= x / r / r + x / r + x

T <= x * (1 / r / r + 1 / r + 1)

T / (1 / r / r + 1 / r + 1) <= x

3 . , , :

T / (1 / r / r / r + 1 / r / r + 1 / r + 1) <= x

, T / m, T / (m / r + 1).

, .

, , .... !

def getFixedPayment(balance, annual_interest_rate, counter=12, interest=...):

    if counter == 1:
        return roundup(balance / interest)

    monthly_interest_rate = annual_interest_rate / 12
    r = 1 + monthly_interest_rate

    return getFixedPayment(balance, annual_interest_rate, counter - 1, ...)

, . calc.py, python -mdoctest calc.py. , , , .

def getFixedPayment(balance, annual_interest_rate, counter=12, interest=...):
    """
    >>> getFixedPayment(3329, 0.2)
    310

    >>> getFixedPayment(4773, 0.2)
    440

    >>> getFixedPayment(3926, 0.2)
    360

    >>> getFixedPayment(265, 0.18)
    30

    >>> getFixedPayment(263, 0.18)
    30

    >>> getFixedPayment(317, 0.25)
    30

    >>> getFixedPayment(720, 0.2)
    70

    >>> getFixedPayment(4284, 0.2)
    400

    >>> getFixedPayment(3834, 0.15)
    350

    >>> getFixedPayment(3045, 0.18)
    280

    >>> getFixedPayment(4461, 0.2)
    410

    >>> getFixedPayment(4657, 0.04)
    400

    >>> getFixedPayment(3395, 0.2)
    310

    >>> getFixedPayment(4045, 0.15)
    370

    >>> getFixedPayment(3963, 0.18)
    360

    """

    if counter == 1:
        return roundup(balance / interest)

    monthly_interest_rate = annual_interest_rate / 12
    r = 1 + monthly_interest_rate

    return getFixedPayment(balance, annual_interest_rate, counter - 1, ...)
+3

:

,

:

totalLoan = balance + (balance*annualInterestRate)

totalLoan . .

. , :

def balance_after_a_year(balance, monthly_payment, annual_interest_rate):
    monthly_interest_rate = annual_interest_rate / 12.0
    for month in range(12):
        balance = (balance - monthly_payment) * (1 + monthly_interest_rate)
    return balance

print(balance_after_a_year(3834, 340, 0.15))
# 23.153402858591026
print(balance_after_a_year(3834, 350, 0.15))
# -107.05775649703746

. 0 balance 10 , :

def getFixedPayment(b,r):
    return next(m for m in range(0, b, 10) if balance_after_a_year(b, m, r) <= 0)
print(getFixedPayment(3834, 0.15))
# 350

, @janos.

, .

:

def getFixedPayment(b,r):
    m = r / 12.0
    return m * b / (1 - (1 + m)**-12)
print(getFixedPayment(3834, 0.15))
# 346.05036953133276

!

+2

Source: https://habr.com/ru/post/1689518/


All Articles