diff --git a/solution.py b/solution.py new file mode 100644 index 0000000..962aabe --- /dev/null +++ b/solution.py @@ -0,0 +1,34 @@ +def derivative(x): # Derivative function of polynomial + ans = 0 + for i in range(n): + degree = n - i - 1 + ans += degree * cash_flow[i] * x ** (degree - 1) + return ans + + +def f(x): # Polynomial + ans = 0 + for i in range(n): + degree = n - i - 1 + ans += cash_flow[i] * x ** degree + ans -= market_value[-1] # Last market value + return ans + + +def solution(): + current = 1.5 # Guess for initial interest (current = 1 + x, therefore initial guess is 0.5%) + x = 500 # Adjust to higher value for more precision + for i in range(x): + current = current - (f(current) / derivative(current)) + return current - 1 + + +data = open("test2.csv").readlines() +cash_flow = [] +market_value = [] +for line in data[1:]: + a, b, c = [float(i) for i in line.split(",")] + cash_flow.append(b) + market_value.append(c) +n = len(cash_flow) +print(solution()) diff --git a/test2.csv b/test2.csv new file mode 100644 index 0000000..8e23876 --- /dev/null +++ b/test2.csv @@ -0,0 +1,7 @@ + +0,1000,1000 +1,1500,2600 +2,0,2700 +3,0,3000 +4,0,3400 +5,0,3806 \ No newline at end of file