Skip to content
Closed
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
1 change: 1 addition & 0 deletions algorithms/maths/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,4 @@
from .magic_number import *
from .krishnamurthy_number import *
from .num_perfect_squares import *
from .return_on_investment import *
53 changes: 53 additions & 0 deletions algorithms/maths/return_on_investment.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
"""
Return on Investment (ROI)

ROI measures the profitability of an investment relative to its cost,
expressed as a percentage.

Formula: ROI = (Gain - Cost) / Cost * 100

Reference: https://www.investopedia.com/terms/r/returnoninvestment.asp

Complexity:
Time: O(1)
Space: O(1)
"""

from __future__ import annotations


def return_on_investment(
gain_from_investment: float, cost_of_investment: float
) -> float:
"""
Calculate return on investment as a percentage.

Args:
gain_from_investment: Total value gained from the investment.
cost_of_investment: Total cost of the investment.

Returns:
ROI as a percentage.

Raises:
ValueError: If cost_of_investment is not positive.

Examples:
>>> return_on_investment(1000.0, 500.0)
100.0
>>> return_on_investment(500.0, 500.0)
0.0
>>> return_on_investment(200.0, 500.0)
-60.0
>>> return_on_investment(0.0, 500.0)
-100.0
>>> return_on_investment(1000.0, 0.0)
Traceback (most recent call last):
...
ValueError: cost_of_investment must be greater than 0
"""
if cost_of_investment <= 0:
raise ValueError("cost_of_investment must be greater than 0")
return round(
(gain_from_investment - cost_of_investment) / cost_of_investment * 100, 2
)