|
| 1 | +# https://www.investopedia.com |
| 2 | + |
| 3 | +from __future__ import annotations |
| 4 | + |
| 5 | + |
| 6 | +def simple_interest( |
| 7 | + principle: float, daily_interest_rate: float, days_between_payments: int |
| 8 | +) -> float: |
| 9 | + """ |
| 10 | + >>> simple_interest(18000.0, 0.06, 3) |
| 11 | + 3240.0 |
| 12 | + >>> simple_interest(0.5, 0.06, 3) |
| 13 | + 0.09 |
| 14 | + >>> simple_interest(18000.0, 0.01, 10) |
| 15 | + 1800.0 |
| 16 | + >>> simple_interest(18000.0, 0.0, 3) |
| 17 | + 0.0 |
| 18 | + >>> simple_interest(5500.0, 0.01, 100) |
| 19 | + 5500.0 |
| 20 | + >>> simple_interest(10000.0, -0.06, 3) |
| 21 | + Traceback (most recent call last): |
| 22 | + ... |
| 23 | + ValueError: daily_interest_rate must be >= 0 |
| 24 | + >>> simple_interest(-10000.0, 0.06, 3) |
| 25 | + Traceback (most recent call last): |
| 26 | + ... |
| 27 | + ValueError: principle must be > 0 |
| 28 | + >>> simple_interest(5500.0, 0.01, -5) |
| 29 | + Traceback (most recent call last): |
| 30 | + ... |
| 31 | + ValueError: days_between_payments must be > 0 |
| 32 | + """ |
| 33 | + if days_between_payments <= 0: |
| 34 | + raise ValueError("days_between_payments must be > 0") |
| 35 | + if daily_interest_rate < 0: |
| 36 | + raise ValueError("daily_interest_rate must be >= 0") |
| 37 | + if principle <= 0: |
| 38 | + raise ValueError("principle must be > 0") |
| 39 | + return principle * daily_interest_rate * days_between_payments |
| 40 | + |
| 41 | + |
| 42 | +def compound_interest( |
| 43 | + principle: float, |
| 44 | + nominal_annual_interest_rate_percentage: float, |
| 45 | + number_of_compounding_periods: int, |
| 46 | +) -> float: |
| 47 | + """ |
| 48 | + >>> compound_interest(10000.0, 0.05, 3) |
| 49 | + 1576.2500000000014 |
| 50 | + >>> compound_interest(10000.0, 0.05, 1) |
| 51 | + 500.00000000000045 |
| 52 | + >>> compound_interest(0.5, 0.05, 3) |
| 53 | + 0.07881250000000006 |
| 54 | + >>> compound_interest(10000.0, 0.06, -4) |
| 55 | + Traceback (most recent call last): |
| 56 | + ... |
| 57 | + ValueError: number_of_compounding_periods must be > 0 |
| 58 | + >>> compound_interest(10000.0, -3.5, 3.0) |
| 59 | + Traceback (most recent call last): |
| 60 | + ... |
| 61 | + ValueError: nominal_annual_interest_rate_percentage must be >= 0 |
| 62 | + >>> compound_interest(-5500.0, 0.01, 5) |
| 63 | + Traceback (most recent call last): |
| 64 | + ... |
| 65 | + ValueError: principle must be > 0 |
| 66 | + """ |
| 67 | + if number_of_compounding_periods <= 0: |
| 68 | + raise ValueError("number_of_compounding_periods must be > 0") |
| 69 | + if nominal_annual_interest_rate_percentage < 0: |
| 70 | + raise ValueError("nominal_annual_interest_rate_percentage must be >= 0") |
| 71 | + if principle <= 0: |
| 72 | + raise ValueError("principle must be > 0") |
| 73 | + |
| 74 | + return principle * ( |
| 75 | + (1 + nominal_annual_interest_rate_percentage) ** number_of_compounding_periods |
| 76 | + - 1 |
| 77 | + ) |
| 78 | + |
| 79 | + |
| 80 | +if __name__ == "__main__": |
| 81 | + import doctest |
| 82 | + |
| 83 | + doctest.testmod() |
0 commit comments