Skip to content
5 changes: 3 additions & 2 deletions bit_manipulation/index_of_rightmost_set_bit.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@
def get_index_of_rightmost_set_bit(number: int) -> int:
"""
Take in a positive integer 'number'.
Returns the zero-based index of first set bit in that 'number' from right.
Returns -1, If no set bit found.
Return the zero-based index of the first set bit in that 'number'
starting from right.
Return -1 if the set bit is not found.

>>> get_index_of_rightmost_set_bit(0)
-1
Expand Down
51 changes: 51 additions & 0 deletions financial/dividend_discount_model.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
"""
Program using Dividend Discount Model to pricing a company's stock, given
- Value of the next year dividend
- Constant cost of equity capital
- constant growth rate in perpetuity

Wikipedia Reference: https://en.wikipedia.org/wiki/Dividend_discount_model
"""


def dividend_discount_model(
next_dividend: float, constant_cost: float, constant_growth: float
) -> float:
"""
Formula for Dividend Discount Model:
P = D1/(r-g)
where P is the expected price of a company's stock,
r is the rate of interest per month,
and n is the number of payments

>>> dividend_discount_model(25000, 0.12, 0.03)
277777.7777777778
>>> dividend_discount_model(25000, 0.12, 0.10)
1250000.0000000007
>>> dividend_discount_model(0, 0.12, 0.03)
Traceback (most recent call last):
...
ValueError: The next year's dividend must be > 0
>>> dividend_discount_model(25000, -0.12, 0.03)
Traceback (most recent call last):
...
ValueError: The rate of constant cost must be >= 0
>>> dividend_discount_model(25000, 0.12, 0)
Traceback (most recent call last):
...
ValueError: The constant growth must be >= 0
"""
if next_dividend <= 0:
raise ValueError("The next year's dividend must be > 0")
if constant_cost < 0:
raise ValueError("The rate of constant cost must be >= 0")
if constant_growth <= 0:
raise ValueError("The constant growth must be >= 0")

return next_dividend / (constant_cost - constant_growth)


if __name__ == "__main__":
import doctest

doctest.testmod()
11 changes: 10 additions & 1 deletion financial/price_plus_tax.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
"""
Calculate price plus tax of a good or service given its price and a tax rate.
Calculate price plus tax of a good or service given
- Price
- Tax rate
"""


Expand All @@ -10,6 +12,13 @@ def price_plus_tax(price: float, tax_rate: float) -> float:
>>> price_plus_tax(125.50, 0.05)
131.775
"""
# If the price is negative, it will raise a warning
if price <= 0:
raise Exception("The price must be >= 0")
# If the tax rate is negative or zero, it will raise a warning
if tax_rate <= 0:
raise Exception("The tax rate must be > 0")

return price * (1 + tax_rate)


Expand Down