--- a/progs/drumb_sol.scala Wed Nov 16 15:05:40 2016 +0000
+++ b/progs/drumb_sol.scala Wed Nov 16 18:48:00 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