From bcfd231c898300e59ea3f4b193ba4941aa1cf5b8 Mon Sep 17 00:00:00 2001 From: ADITYA-KUMAR-2358 Date: Fri, 3 Oct 2025 00:53:07 +0530 Subject: [PATCH 1/2] simplify apr_interest with validation helper and constants --- .vscode/settings.json | 5 +++- financial/interest.py | 63 ++++++++++++++++++++++++------------------- 2 files changed, 40 insertions(+), 28 deletions(-) diff --git a/.vscode/settings.json b/.vscode/settings.json index ef16fa1aa7ac..0dd539622848 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -1,5 +1,8 @@ { "githubPullRequests.ignoredPullRequestBranches": [ "master" - ] + ], + "python-envs.defaultEnvManager": "ms-python.python:conda", + "python-envs.defaultPackageManager": "ms-python.python:conda", + "python-envs.pythonProjects": [] } diff --git a/financial/interest.py b/financial/interest.py index 33d02e27ccb3..56ddadb2a211 100644 --- a/financial/interest.py +++ b/financial/interest.py @@ -83,35 +83,44 @@ def apr_interest( 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 + + 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}") + else: + if value <= min_value: + raise ValueError(f"{name} must be > {min_value}") + + 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) - return compound_interest( - principal, nominal_annual_percentage_rate / 365, number_of_years * 365 - ) if __name__ == "__main__": From 700e9f10bea1960877bfff90b8e44a48f2889365 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Thu, 2 Oct 2025 19:27:14 +0000 Subject: [PATCH 2/2] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- financial/interest.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/financial/interest.py b/financial/interest.py index 56ddadb2a211..4d7d62a44848 100644 --- a/financial/interest.py +++ b/financial/interest.py @@ -104,7 +104,9 @@ def apr_interest( """ DAYS_IN_YEAR = 365 - def validate(name: str, value: float, min_value: float, include_equal: bool = False): + 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}") @@ -122,7 +124,6 @@ def validate(name: str, value: float, min_value: float, include_equal: bool = Fa return compound_interest(principal, daily_rate, total_days) - if __name__ == "__main__": import doctest