Skip to content

Commit c41bb49

Browse files
tarcisiobrunicclausspoyea
authored
Financials (TheAlgorithms#5585)
* feat: creates math calculations for financials * refactor: make pull request items requirements * refactor: provides type hint for parameters * refactor: applies code review suggestions * refactor: adds more examples tests * refactor: throws ValueError instead of Exception * refactor: fix formatting * refactor: fix formatting * Update interest.py * Update and rename financials/ABOUT.md to financial/ABOUT.md * Rename financials/__init__.py to financial/__init__.py * Rename financials/interest.py to financial/interest.py * https://www.investopedia.com * Update __init__.py * pre-commit: Disable end-of-file-fixer * Revert change to pre-commit * Update __init__.py * __init__.py Co-authored-by: Christian Clauss <[email protected]> Co-authored-by: John Law <[email protected]>
1 parent 23f43af commit c41bb49

File tree

3 files changed

+87
-0
lines changed

3 files changed

+87
-0
lines changed

financial/ABOUT.md

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
### Interest
2+
3+
* Compound Interest: "Compound interest is calculated by multiplying the initial principal amount by one plus the annual interest rate raised to the number of compound periods minus one." [Compound Interest](https://www.investopedia.com/)
4+
* Simple Interest: "Simple interest paid or received over a certain period is a fixed percentage of the principal amount that was borrowed or lent. " [Simple Interest](https://www.investopedia.com/)

financial/__init__.py

Whitespace-only changes.

financial/interest.py

+83
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
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

Comments
 (0)