From 603a98c2ccf2fcb6ee523cfd28de2c33ceef435f Mon Sep 17 00:00:00 2001 From: joma Date: Sat, 17 Sep 2016 15:32:22 -0400 Subject: [PATCH] YE DAWG --- your_return_calculator.rb | 31 ++++++++++++++++++++++--------- 1 file changed, 22 insertions(+), 9 deletions(-) diff --git a/your_return_calculator.rb b/your_return_calculator.rb index 3b43174..e1b3bbe 100644 --- a/your_return_calculator.rb +++ b/your_return_calculator.rb @@ -2,14 +2,27 @@ class YourReturnCalculator < ReturnCalculator def calculate! - # Write your code here. - # You have access to the `snapshots` variable. - # - # You can access the following properties of a snapshot: - # snapshot.date - # snapshot.cash_flow - # snapshot.market_value - - BigDecimal.new(0) + $length = snapshots.length + $i = 0 + subperiods = Array.new + while $i < $length - 1 do + market_value_1 = snapshots[$i].market_value + market_value_2 = snapshots[$i+1].market_value + cash_flow_2 = snapshots[$i+1].cash_flow + if market_value_1 == 0 + return 0 + end + subperiod = ((market_value_2 - cash_flow_2)/market_value_1) - 1 + subperiods.push(subperiod) + $i += 1 + end + # geo linking time + answer = 1 + $i = 0 + while $i < $length - 1 do + answer *= (1 + subperiods[$i]) + $i += 1 + end + BigDecimal.new(answer - 1) end end