main_solution1/drumb.scala
changeset 431 ef68136b9a96
parent 418 fa7f7144f2bb
--- a/main_solution1/drumb.scala	Wed Nov 02 21:49:42 2022 +0000
+++ b/main_solution1/drumb.scala	Thu Nov 03 09:55:11 2022 +0000
@@ -123,40 +123,14 @@
 
 def yearly_yield(data: List[List[Option[Double]]], balance: Long, index: Int): Long = {
   val somes = data(index).flatten
-  println(s"somes: $somes")
   val somes_length = somes.length
   if (somes_length == 0) balance
   else {
     val portion: Double = balance.toDouble / somes_length.toDouble
-    println(s"portion: $portion")
-    val b = balance + (for (x <- somes) yield (x * portion)).sum.toLong
-    println(s"balance $b")
-    println(s"profit ${(for (x <- somes) yield (x * portion)).sum}")
-    b
+    balance + (for (x <- somes) yield (x * portion)).sum.toLong
   }
 }
 
-val dd = List(List(Some(0.9)), List(Some(0.9)))
-yearly_yield(dd, 100, 0)
-
-def yearly_yield2(data: List[List[Option[Double]]], balance: Long, index: Int) : Long = {
-
-    val share = balance/data(index).flatten.size.toDouble
-    println(s"portion: $share")
-    def count(data: List[Option[Double]], prof: Double, share: Double) : Double = data match{
-        case Nil => prof
-        case x::rest  => count(rest,prof + (x.getOrElse(0.0) * share), share)
-    }
-    val balance_double = count(data(index), 0.0, share)
-    println(balance_double)
-    println(s"balance ${balance_double + balance}")
-    println(s"profit $balance_double")
-    //if (balance_double >= balance_double.toLong.toDouble+0.5) (balance_double + 1).toLong else balance_double.toLong
-    (balance_double.toLong + balance)
-}
-
-yearly_yield2(dd, 100, 0)
-
 // test case using the deltas calculated above
 //println("Task 6 yield from Google and Apple in 2010 with  balance 100")
 
@@ -166,7 +140,6 @@
 
 
 //val goog_aapl_yield = yearly_yield(goog_aapl_deltas, 100, 0)
-//val goog_aapl_yield = yearly_yield2(goog_aapl_deltas, 100, 0)
 //println("Rounded yield: " ++ goog_aapl_yield.toString ++ "\n")
 
 
@@ -188,20 +161,10 @@
   }
 }
 
-def compound_yield2(data: List[List[Option[Double]]], balance: Long, index: Int): Long = {
-  if (index >= data.length) balance else {
-    val new_balance = yearly_yield2(data, balance, index)
-    compound_yield2(data, new_balance, index + 1)
-  }
-}
-
 def investment(portfolio: List[String], years: Range, start_balance: Long): Long = {
   compound_yield(get_deltas(get_prices(portfolio, years)), start_balance, 0)
 }
 
-def investment2(portfolio: List[String], years: Range, start_balance: Long): Long = {
-  compound_yield2(get_deltas(get_prices(portfolio, years)), start_balance, 0)
-}
 
 
 //test cases for the two portfolios given above
@@ -213,25 +176,6 @@
 }
 
 
-investment(List("GOOG", "AAPL", "BIDU"), 2000 to 2000, 100)
-investment2(List("GOOG", "AAPL", "BIDU"), 2000 to 2000, 100)
-
-investment(List("GOOG", "AAPL", "BIDU"), 2000 to 2001, 100)
-investment2(List("GOOG", "AAPL", "BIDU"), 2000 to 2001, 100)
-
-investment(List("GOOG", "AAPL", "BIDU"), 2000 to 2002, 100)
-investment2(List("GOOG", "AAPL", "BIDU"), 2000 to 2002, 100) 
-
-investment(List("GOOG", "AAPL", "BIDU"), 2000 to 2003, 100)
-investment2(List("GOOG", "AAPL", "BIDU"), 2000 to 2003, 100) 
-
-investment(List("GOOG", "AAPL", "BIDU"), 2000 to 2004, 100) 
-investment2(List("GOOG", "AAPL", "BIDU"), 2000 to 2004, 100) 
-
-investment(List("GOOG", "AAPL", "BIDU"), 2000 to 2005, 100) 
-investment2(List("GOOG", "AAPL", "BIDU"), 2000 to 2005, 100) 
-
-
 
 
 
@@ -248,3 +192,5 @@
 
 
 
+
+