# HG changeset patch # User Christian Urban # Date 1479339276 0 # Node ID 34153e0485fc727e40dc5e94cb44db7f34ef54a9 # Parent 9f875191256090b9fd2cbfbaa33ef47fa7c8e719# Parent 7a4fe3f6b1883863b50e864b2dc93a810c32f61a merged diff -r 9f8751912560 -r 34153e0485fc progs/drumb_sol.scala --- a/progs/drumb_sol.scala Wed Nov 16 23:34:05 2016 +0000 +++ b/progs/drumb_sol.scala Wed Nov 16 23:34:36 2016 +0000 @@ -42,7 +42,8 @@ // test case -//val p = get_prices(List("GOOG", "AAPL"), 2010 to 2012) +val p = get_prices(List("FB"), 2012 to 2014) +val p = get_prices(List("GOOG", "AAPL"), 2005 to 2012) // (2) The first function below calculates the change factor (delta) between @@ -62,7 +63,7 @@ // test case using the prices calculated above -//val d = get_deltas(p) +val d = get_deltas(p) // (3) Write a function that given change factors, a starting balance and a year @@ -78,13 +79,21 @@ val somes_length = somes.length if (somes_length == 0) balance else { - val portion: Double = balance / somes_length + val portion: Double = (balance.toDouble) / somes_length balance + (for (x <- somes) yield (x * portion)).sum.toLong } } //test case +yearly_yield(d, 100, 0) +yearly_yield(d, 225, 1) +yearly_yield(d, 246, 2) +yearly_yield(d, 466, 3) +yearly_yield(d, 218, 4) +yearly_yield(d, 469, 5) +yearly_yield(d, 587, 6) //yearly_yield(d, 100, 0) +//yearly_yield(d, 125, 1) def compound_yield(data: List[List[Option[Double]]], balance: Long, year: Int): Long = { if (year >= data.length) balance else { @@ -93,10 +102,13 @@ } } +compound_yield(d.take(6), 100, 0) + def investment(portfolio: List[String], years: Range, start_balance: Long): Long = { compound_yield(get_deltas(get_prices(portfolio, years)), start_balance, 0) } +investment(List("GOOG", "AAPL"), 2005 to 2009, 100) //test cases for the two portfolios given above