Skip to content

Commit bcfd231

Browse files
simplify apr_interest with validation helper and constants
1 parent a71618f commit bcfd231

2 files changed

Lines changed: 40 additions & 28 deletions

File tree

.vscode/settings.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
{
22
"githubPullRequests.ignoredPullRequestBranches": [
33
"master"
4-
]
4+
],
5+
"python-envs.defaultEnvManager": "ms-python.python:conda",
6+
"python-envs.defaultPackageManager": "ms-python.python:conda",
7+
"python-envs.pythonProjects": []
58
}

financial/interest.py

Lines changed: 36 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -83,35 +83,44 @@ def apr_interest(
8383
number_of_years: float,
8484
) -> float:
8585
"""
86-
>>> apr_interest(10000.0, 0.05, 3)
87-
1618.223072263547
88-
>>> apr_interest(10000.0, 0.05, 1)
89-
512.6749646744732
90-
>>> apr_interest(0.5, 0.05, 3)
91-
0.08091115361317736
92-
>>> apr_interest(10000.0, 0.06, -4)
93-
Traceback (most recent call last):
94-
...
95-
ValueError: number_of_years must be > 0
96-
>>> apr_interest(10000.0, -3.5, 3.0)
97-
Traceback (most recent call last):
98-
...
99-
ValueError: nominal_annual_percentage_rate must be >= 0
100-
>>> apr_interest(-5500.0, 0.01, 5)
101-
Traceback (most recent call last):
102-
...
103-
ValueError: principal must be > 0
86+
Calculate the interest earned using daily compounded APR.
87+
88+
Args:
89+
principal: Initial amount of money (must be > 0).
90+
nominal_annual_percentage_rate: APR as a decimal (must be >= 0).
91+
number_of_years: Number of years money is invested (must be > 0).
92+
93+
Returns:
94+
Total interest earned.
95+
96+
Raises:
97+
ValueError: If any input is invalid.
98+
99+
Examples:
100+
>>> apr_interest(10000.0, 0.05, 3)
101+
1618.223072263547
102+
>>> apr_interest(10000.0, 0.05, 1)
103+
512.6749646744732
104104
"""
105-
if number_of_years <= 0:
106-
raise ValueError("number_of_years must be > 0")
107-
if nominal_annual_percentage_rate < 0:
108-
raise ValueError("nominal_annual_percentage_rate must be >= 0")
109-
if principal <= 0:
110-
raise ValueError("principal must be > 0")
105+
DAYS_IN_YEAR = 365
106+
107+
def validate(name: str, value: float, min_value: float, include_equal: bool = False):
108+
if include_equal:
109+
if value < min_value:
110+
raise ValueError(f"{name} must be >= {min_value}")
111+
else:
112+
if value <= min_value:
113+
raise ValueError(f"{name} must be > {min_value}")
114+
115+
validate("principal", principal, 0)
116+
validate("nominal_annual_percentage_rate", nominal_annual_percentage_rate, 0, True)
117+
validate("number_of_years", number_of_years, 0)
118+
119+
daily_rate = nominal_annual_percentage_rate / DAYS_IN_YEAR
120+
total_days = number_of_years * DAYS_IN_YEAR
121+
122+
return compound_interest(principal, daily_rate, total_days)
111123

112-
return compound_interest(
113-
principal, nominal_annual_percentage_rate / 365, number_of_years * 365
114-
)
115124

116125

117126
if __name__ == "__main__":

0 commit comments

Comments
 (0)