Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
{
"githubPullRequests.ignoredPullRequestBranches": [
"master"
]
],
"python-envs.defaultEnvManager": "ms-python.python:conda",
"python-envs.defaultPackageManager": "ms-python.python:conda",
"python-envs.pythonProjects": []
}
64 changes: 37 additions & 27 deletions financial/interest.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,35 +83,45 @@
number_of_years: float,
) -> float:
"""
>>> apr_interest(10000.0, 0.05, 3)
1618.223072263547
>>> apr_interest(10000.0, 0.05, 1)
512.6749646744732
>>> apr_interest(0.5, 0.05, 3)
0.08091115361317736
>>> apr_interest(10000.0, 0.06, -4)
Traceback (most recent call last):
...
ValueError: number_of_years must be > 0
>>> apr_interest(10000.0, -3.5, 3.0)
Traceback (most recent call last):
...
ValueError: nominal_annual_percentage_rate must be >= 0
>>> apr_interest(-5500.0, 0.01, 5)
Traceback (most recent call last):
...
ValueError: principal must be > 0
Calculate the interest earned using daily compounded APR.

Args:
principal: Initial amount of money (must be > 0).
nominal_annual_percentage_rate: APR as a decimal (must be >= 0).
number_of_years: Number of years money is invested (must be > 0).

Returns:
Total interest earned.

Raises:
ValueError: If any input is invalid.

Examples:
>>> apr_interest(10000.0, 0.05, 3)
1618.223072263547
>>> apr_interest(10000.0, 0.05, 1)
512.6749646744732
"""
if number_of_years <= 0:
raise ValueError("number_of_years must be > 0")
if nominal_annual_percentage_rate < 0:
raise ValueError("nominal_annual_percentage_rate must be >= 0")
if principal <= 0:
raise ValueError("principal must be > 0")
DAYS_IN_YEAR = 365

Check failure on line 105 in financial/interest.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (N806)

financial/interest.py:105:5: N806 Variable `DAYS_IN_YEAR` in function should be lowercase

return compound_interest(
principal, nominal_annual_percentage_rate / 365, number_of_years * 365
)
def validate(
name: str, value: float, min_value: float, include_equal: bool = False
):
if include_equal:
if value < min_value:
raise ValueError(f"{name} must be >= {min_value}")

Check failure on line 112 in financial/interest.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (EM102)

financial/interest.py:112:34: EM102 Exception must not use an f-string literal, assign to variable first
else:
if value <= min_value:

Check failure on line 114 in financial/interest.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (PLR5501)

financial/interest.py:113:9: PLR5501 Use `elif` instead of `else` then `if`, to reduce indentation
raise ValueError(f"{name} must be > {min_value}")

Check failure on line 115 in financial/interest.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (EM102)

financial/interest.py:115:34: EM102 Exception must not use an f-string literal, assign to variable first

validate("principal", principal, 0)
validate("nominal_annual_percentage_rate", nominal_annual_percentage_rate, 0, True)
validate("number_of_years", number_of_years, 0)

daily_rate = nominal_annual_percentage_rate / DAYS_IN_YEAR
total_days = number_of_years * DAYS_IN_YEAR

return compound_interest(principal, daily_rate, total_days)


if __name__ == "__main__":
Expand Down
Loading