#!/usr/bin/env python3

"""Compound Interest.

Suppose that you invest $10,000 for 10 years at an interest rate of 6%
compounded monthly. What will be the value of your investment at the
end of the 10 year period?

Create a function that accepts the principal p, the term in years t, 
the interest rate r, and the number of compounding periods per year n. 
The function returns the value at the end of term rounded to the nearest cent.

For the example above:

compound_interest(10000, 10, 0.06, 12) ➞ 18193.97

Note that the interest rate is given as a decimal and n=12 
because with monthly compounding there are 12 periods per year. 
Compounding can also be done annually, quarterly, weekly, or daily.

Source:
https://edabit.com/challenge/HjpihSKFBfRCCg86J
"""

from decimal import Decimal
from operator import truediv


def compound_interest(p, t, r, n):
    """Return the value at the end of term rounded to the nearest cent."""
    interest = Decimal(p * (1 + truediv(r, n))**(n*t))
    return float(round(interest, 2))


def main():
    """Run sample functions. Do not import."""
    assert compound_interest(100, 1, 0.05, 1) == 105.0
    assert compound_interest(3500, 15, 0.1, 4) == 15399.26
    assert compound_interest(100000, 20, 0.15, 365) == 2007316.26
    print('Passed.')


if __name__ == "__main__":
    main()
