2021-10-26 16:19:00 +00:00
|
|
|
# https://www.investopedia.com
|
|
|
|
|
|
|
|
from __future__ import annotations
|
|
|
|
|
|
|
|
|
|
|
|
def simple_interest(
|
2021-10-26 16:41:32 +00:00
|
|
|
principal: float, daily_interest_rate: float, days_between_payments: int
|
2021-10-26 16:19:00 +00:00
|
|
|
) -> float:
|
|
|
|
"""
|
|
|
|
>>> simple_interest(18000.0, 0.06, 3)
|
|
|
|
3240.0
|
|
|
|
>>> simple_interest(0.5, 0.06, 3)
|
|
|
|
0.09
|
|
|
|
>>> simple_interest(18000.0, 0.01, 10)
|
|
|
|
1800.0
|
|
|
|
>>> simple_interest(18000.0, 0.0, 3)
|
|
|
|
0.0
|
|
|
|
>>> simple_interest(5500.0, 0.01, 100)
|
|
|
|
5500.0
|
|
|
|
>>> simple_interest(10000.0, -0.06, 3)
|
|
|
|
Traceback (most recent call last):
|
|
|
|
...
|
|
|
|
ValueError: daily_interest_rate must be >= 0
|
|
|
|
>>> simple_interest(-10000.0, 0.06, 3)
|
|
|
|
Traceback (most recent call last):
|
|
|
|
...
|
2021-10-26 16:41:32 +00:00
|
|
|
ValueError: principal must be > 0
|
2021-10-26 16:19:00 +00:00
|
|
|
>>> simple_interest(5500.0, 0.01, -5)
|
|
|
|
Traceback (most recent call last):
|
|
|
|
...
|
|
|
|
ValueError: days_between_payments must be > 0
|
|
|
|
"""
|
|
|
|
if days_between_payments <= 0:
|
|
|
|
raise ValueError("days_between_payments must be > 0")
|
|
|
|
if daily_interest_rate < 0:
|
|
|
|
raise ValueError("daily_interest_rate must be >= 0")
|
2021-10-26 16:41:32 +00:00
|
|
|
if principal <= 0:
|
|
|
|
raise ValueError("principal must be > 0")
|
|
|
|
return principal * daily_interest_rate * days_between_payments
|
2021-10-26 16:19:00 +00:00
|
|
|
|
|
|
|
|
|
|
|
def compound_interest(
|
2021-10-26 16:41:32 +00:00
|
|
|
principal: float,
|
2021-10-26 16:19:00 +00:00
|
|
|
nominal_annual_interest_rate_percentage: float,
|
|
|
|
number_of_compounding_periods: int,
|
|
|
|
) -> float:
|
|
|
|
"""
|
|
|
|
>>> compound_interest(10000.0, 0.05, 3)
|
|
|
|
1576.2500000000014
|
|
|
|
>>> compound_interest(10000.0, 0.05, 1)
|
|
|
|
500.00000000000045
|
|
|
|
>>> compound_interest(0.5, 0.05, 3)
|
|
|
|
0.07881250000000006
|
|
|
|
>>> compound_interest(10000.0, 0.06, -4)
|
|
|
|
Traceback (most recent call last):
|
|
|
|
...
|
|
|
|
ValueError: number_of_compounding_periods must be > 0
|
|
|
|
>>> compound_interest(10000.0, -3.5, 3.0)
|
|
|
|
Traceback (most recent call last):
|
|
|
|
...
|
|
|
|
ValueError: nominal_annual_interest_rate_percentage must be >= 0
|
|
|
|
>>> compound_interest(-5500.0, 0.01, 5)
|
|
|
|
Traceback (most recent call last):
|
|
|
|
...
|
2021-10-26 16:41:32 +00:00
|
|
|
ValueError: principal must be > 0
|
2021-10-26 16:19:00 +00:00
|
|
|
"""
|
|
|
|
if number_of_compounding_periods <= 0:
|
|
|
|
raise ValueError("number_of_compounding_periods must be > 0")
|
|
|
|
if nominal_annual_interest_rate_percentage < 0:
|
|
|
|
raise ValueError("nominal_annual_interest_rate_percentage must be >= 0")
|
2021-10-26 16:41:32 +00:00
|
|
|
if principal <= 0:
|
|
|
|
raise ValueError("principal must be > 0")
|
2021-10-26 16:19:00 +00:00
|
|
|
|
2021-10-26 16:41:32 +00:00
|
|
|
return principal * (
|
2021-10-26 16:19:00 +00:00
|
|
|
(1 + nominal_annual_interest_rate_percentage) ** number_of_compounding_periods
|
|
|
|
- 1
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
import doctest
|
|
|
|
|
|
|
|
doctest.testmod()
|