>>> present_value(0.13, [10, 20.70, -293, 297]) 4.69 >>> present_value(0.07, [-109129.39, 30923.23, 15098.93, 29734,39]) -42739.63 >>> present_value(0.07, [109129.39, 30923.23, 15098.93, 29734,39]) 175519.15 >>> present_value(-1, [109129.39, 30923.23, 15098.93, 29734,39]
(discount_rate: float, cash_flows: list[float])
| 10 | |
| 11 | |
| 12 | def present_value(discount_rate: float, cash_flows: list[float]) -> float: |
| 13 | """ |
| 14 | >>> present_value(0.13, [10, 20.70, -293, 297]) |
| 15 | 4.69 |
| 16 | >>> present_value(0.07, [-109129.39, 30923.23, 15098.93, 29734,39]) |
| 17 | -42739.63 |
| 18 | >>> present_value(0.07, [109129.39, 30923.23, 15098.93, 29734,39]) |
| 19 | 175519.15 |
| 20 | >>> present_value(-1, [109129.39, 30923.23, 15098.93, 29734,39]) |
| 21 | Traceback (most recent call last): |
| 22 | ... |
| 23 | ValueError: Discount rate cannot be negative |
| 24 | >>> present_value(0.03, []) |
| 25 | Traceback (most recent call last): |
| 26 | ... |
| 27 | ValueError: Cash flows list cannot be empty |
| 28 | """ |
| 29 | if discount_rate < 0: |
| 30 | raise ValueError("Discount rate cannot be negative") |
| 31 | if not cash_flows: |
| 32 | raise ValueError("Cash flows list cannot be empty") |
| 33 | present_value = sum( |
| 34 | cash_flow / ((1 + discount_rate) ** i) for i, cash_flow in enumerate(cash_flows) |
| 35 | ) |
| 36 | return round(present_value, ndigits=2) |
| 37 | |
| 38 | |
| 39 | if __name__ == "__main__": |
nothing calls this directly
no outgoing calls
no test coverage detected