diff --git a/bit_manipulation/index_of_rightmost_set_bit.py b/bit_manipulation/index_of_rightmost_set_bit.py index c9c911660b08..2e0adb8861ed 100644 --- a/bit_manipulation/index_of_rightmost_set_bit.py +++ b/bit_manipulation/index_of_rightmost_set_bit.py @@ -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 diff --git a/financial/dividend_discount_model.py b/financial/dividend_discount_model.py new file mode 100644 index 000000000000..674593ca0dd9 --- /dev/null +++ b/financial/dividend_discount_model.py @@ -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() diff --git a/financial/price_plus_tax.py b/financial/price_plus_tax.py index 43876d35e57c..2ad8b293d745 100644 --- a/financial/price_plus_tax.py +++ b/financial/price_plus_tax.py @@ -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 """ @@ -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)