reference https www investopedia comtermsppresentvalue asp an algorithm that calculates the present value of a stream of yearly cash flows given 1 the discount rate as a decimal not a percent 2 an array of cash flows with the index of the cash flow being the associated year note this algorithm assumes that cash flows are paid at the end of the specified year presentvalue0 13 10 20 70 293 297 4 69 presentvalue0 07 109129 39 30923 23 15098 93 29734 39 42739 63 presentvalue0 07 109129 39 30923 23 15098 93 29734 39 175519 15 presentvalue1 109129 39 30923 23 15098 93 29734 39 traceback most recent call last valueerror discount rate cannot be negative presentvalue0 03 traceback most recent call last valueerror cash flows list cannot be empty 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 traceback most recent call last valueerror discount rate cannot be negative present_value 0 03 traceback most recent call last valueerror cash flows list cannot be empty
def present_value(discount_rate: float, cash_flows: list[float]) -> float:
    
    if discount_rate < 0:
        raise ValueError("Discount rate cannot be negative")
    if not cash_flows:
        raise ValueError("Cash flows list cannot be empty")
    present_value = sum(
        cash_flow / ((1 + discount_rate) ** i) for i, cash_flow in enumerate(cash_flows)
    )
    return round(present_value, ndigits=2)


if __name__ == "__main__":
    import doctest

    doctest.testmod()