updated
authorChristian Urban <christian.urban@kcl.ac.uk>
Sat, 11 Mar 2023 23:24:15 +0000
changeset 469 48de09728447
parent 468 0587ef444547
child 470 86a456f8cb92
updated
Attic/live.scala
Attic/sudoku_test.scala
Attic/trade.scala
progs/live.scala
progs/sudoku_test.scala
progs/trade.scala
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Attic/live.scala	Sat Mar 11 23:24:15 2023 +0000
@@ -0,0 +1,34 @@
+import scala.annotation.tailrec   
+
+
+def collatz(n: Long): List[Long] =
+  if (n == 1) List(1) else
+    if (n % 2 == 0) (n::collatz(n / 2)) else 
+      (n::collatz(3 * n + 1))
+
+def collatz1(n: Long): Int =
+  if (n == 1) 1 else
+    if (n % 2 == 0) (1 + collatz1(n / 2)) else 
+      (1 + collatz1(3 * n + 1))
+
+@tailrec
+def collatz2(n: Long, acc: Int): Int =
+  if (n == 1) acc else
+    if (n % 2 == 0) collatz2(n / 2, acc + 1) else 
+      collatz2(3 * n + 1, acc + 1)
+
+collatz(1)
+collatz(2)
+collatz(3)
+collatz(4)
+collatz(5)
+collatz(6).length
+collatz(7)
+collatz(8)
+collatz(9).length
+collatz(100000)
+println((for (i <- 1 to 10000000) yield collatz(i).length).max)
+println((for (i <- 1 to 10000000) yield collatz1(i)).max)
+println((for (i <- 1 to 10000000) yield collatz2(i, 1)).max)
+println((for (i <- (1 to 10000000).par) yield collatz2(i, 1)).max)
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Attic/sudoku_test.scala	Sat Mar 11 23:24:15 2023 +0000
@@ -0,0 +1,88 @@
+// Sudoku
+//========
+
+// call parallel version with
+//
+// scala -cp scala-parallel-collections_2.13-0.2.0.jar sudoku_test.scala 
+//
+// or
+//
+// scalac -cp scala-parallel-collections_2.13-0.2.0.jar sudoku_test.scala
+// java -cp .:scala-library-2.13.0.jar:scala-parallel-collections_2.13-0.2.0.jar Sudoku
+
+object Sudoku extends App { 
+
+import scala.collection.parallel.CollectionConverters._
+
+type Pos = (Int, Int)
+val emptyValue = '.'
+val maxValue = 9
+
+val allValues = "123456789".toList
+val indexes = (0 to 8).toList
+
+
+def empty(game: String) = game.indexOf(emptyValue)
+def isDone(game: String) = empty(game) == -1 
+def emptyPosition(game: String) = {
+  val e = empty(game)
+  (e % maxValue, e / maxValue)
+}
+
+
+def get_row(game: String, y: Int) = indexes.map(col => game(y * maxValue + col))
+def get_col(game: String, x: Int) = indexes.map(row => game(x + row * maxValue))
+
+def get_box(game: String, pos: Pos): List[Char] = {
+    def base(p: Int): Int = (p / 3) * 3
+    val x0 = base(pos._1)
+    val y0 = base(pos._2)
+    for (x <- (x0 until x0 + 3).toList;
+         y <- (y0 until y0 + 3).toList) yield game(x + y * maxValue)
+}         
+
+
+def update(game: String, pos: Int, value: Char): String = 
+  game.updated(pos, value)
+
+def toAvoid(game: String, pos: Pos): List[Char] = 
+  (get_col(game, pos._1) ++ get_row(game, pos._2) ++ get_box(game, pos))
+
+def candidates(game: String, pos: Pos): List[Char] = 
+  allValues.diff(toAvoid(game, pos))
+
+def search(game: String): List[String] = {
+  if (isDone(game)) List(game)
+  else {
+    val cs = candidates(game, emptyPosition(game))
+    cs.par.map(c => search(update(game, empty(game), c))).toList.flatten
+  }
+}
+
+
+def pretty(game: String): String = 
+  "\n" + (game.grouped(maxValue).mkString(",\n"))
+
+
+val game2 = """8........
+              |..36.....
+              |.7..9.2..
+              |.5...7...
+              |....457..
+              |...1...3.
+              |..1....68
+              |..85...1.
+              |.9....4..""".stripMargin.replaceAll("\\n", "")
+
+// for measuring time
+def time_needed[T](i: Int, code: => T) = {
+  val start = System.nanoTime()
+  for (j <- 1 to i) code
+  val end = System.nanoTime()
+  s"${(end - start) / 1.0e9} secs"
+}
+
+
+println(time_needed(10, search(game2)))
+
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Attic/trade.scala	Sat Mar 11 23:24:15 2023 +0000
@@ -0,0 +1,56 @@
+// Part 2 about Buy-Low-Sell-High using Yahoo Financial Data
+//===========================================================
+
+
+// (1) Complete the function that is given a list of floats
+// and calculuates the indices for when to buy the commodity 
+// and when to sell
+
+def trade_times(xs: List[Double]): (Int, Int) = ...
+
+
+// an example
+//val prices = List(28.0, 18.0, 20.0, 26.0, 24.0)
+//assert(trade_times(prices) == (1, 3), "the trade_times test fails")
+
+
+// (2) Complete the ``get webpage'' function that takes a
+// a stock symbol as argument and queries the Yahoo server
+// at
+//      http://ichart.yahoo.com/table.csv?s=<<insert stock symbol>>
+// 
+// This servive returns a CSV-list that needs to be separated into
+// a list of strings.
+
+def get_page(symbol: String): List[String] = ...
+
+// (3) Complete the function that processes the CSV list
+// extracting the dates and anjusted close prices. The
+// prices need to be transformed into Doubles.
+
+def process_page(symbol: String): List[(String, Double)] = ...
+
+
+// (4) Complete the query_company function that obtains the
+// processed CSV-list for a stock symbol. It should return
+// the dates for when to buy and sell the stocks of that company.
+
+def query_company(symbol: String): (String, String) =
+
+
+
+// some test cases
+
+//query_company("GOOG")
+
+// some more test cases
+/*
+val indices = List("GOOG", "AAPL", "MSFT", "IBM", "FB", "YHOO", "AMZN", "BIDU")
+
+for (name <- indices) {
+  val times = query_company(name)
+  println(s"Buy ${name} on ${times._1} and sell on ${times._2}")
+}
+*/
+
+
--- a/progs/live.scala	Sat Mar 11 23:22:05 2023 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,34 +0,0 @@
-import scala.annotation.tailrec   
-
-
-def collatz(n: Long): List[Long] =
-  if (n == 1) List(1) else
-    if (n % 2 == 0) (n::collatz(n / 2)) else 
-      (n::collatz(3 * n + 1))
-
-def collatz1(n: Long): Int =
-  if (n == 1) 1 else
-    if (n % 2 == 0) (1 + collatz1(n / 2)) else 
-      (1 + collatz1(3 * n + 1))
-
-@tailrec
-def collatz2(n: Long, acc: Int): Int =
-  if (n == 1) acc else
-    if (n % 2 == 0) collatz2(n / 2, acc + 1) else 
-      collatz2(3 * n + 1, acc + 1)
-
-collatz(1)
-collatz(2)
-collatz(3)
-collatz(4)
-collatz(5)
-collatz(6).length
-collatz(7)
-collatz(8)
-collatz(9).length
-collatz(100000)
-println((for (i <- 1 to 10000000) yield collatz(i).length).max)
-println((for (i <- 1 to 10000000) yield collatz1(i)).max)
-println((for (i <- 1 to 10000000) yield collatz2(i, 1)).max)
-println((for (i <- (1 to 10000000).par) yield collatz2(i, 1)).max)
-
--- a/progs/sudoku_test.scala	Sat Mar 11 23:22:05 2023 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,88 +0,0 @@
-// Sudoku
-//========
-
-// call parallel version with
-//
-// scala -cp scala-parallel-collections_2.13-0.2.0.jar sudoku_test.scala 
-//
-// or
-//
-// scalac -cp scala-parallel-collections_2.13-0.2.0.jar sudoku_test.scala
-// java -cp .:scala-library-2.13.0.jar:scala-parallel-collections_2.13-0.2.0.jar Sudoku
-
-object Sudoku extends App { 
-
-import scala.collection.parallel.CollectionConverters._
-
-type Pos = (Int, Int)
-val emptyValue = '.'
-val maxValue = 9
-
-val allValues = "123456789".toList
-val indexes = (0 to 8).toList
-
-
-def empty(game: String) = game.indexOf(emptyValue)
-def isDone(game: String) = empty(game) == -1 
-def emptyPosition(game: String) = {
-  val e = empty(game)
-  (e % maxValue, e / maxValue)
-}
-
-
-def get_row(game: String, y: Int) = indexes.map(col => game(y * maxValue + col))
-def get_col(game: String, x: Int) = indexes.map(row => game(x + row * maxValue))
-
-def get_box(game: String, pos: Pos): List[Char] = {
-    def base(p: Int): Int = (p / 3) * 3
-    val x0 = base(pos._1)
-    val y0 = base(pos._2)
-    for (x <- (x0 until x0 + 3).toList;
-         y <- (y0 until y0 + 3).toList) yield game(x + y * maxValue)
-}         
-
-
-def update(game: String, pos: Int, value: Char): String = 
-  game.updated(pos, value)
-
-def toAvoid(game: String, pos: Pos): List[Char] = 
-  (get_col(game, pos._1) ++ get_row(game, pos._2) ++ get_box(game, pos))
-
-def candidates(game: String, pos: Pos): List[Char] = 
-  allValues.diff(toAvoid(game, pos))
-
-def search(game: String): List[String] = {
-  if (isDone(game)) List(game)
-  else {
-    val cs = candidates(game, emptyPosition(game))
-    cs.par.map(c => search(update(game, empty(game), c))).toList.flatten
-  }
-}
-
-
-def pretty(game: String): String = 
-  "\n" + (game.grouped(maxValue).mkString(",\n"))
-
-
-val game2 = """8........
-              |..36.....
-              |.7..9.2..
-              |.5...7...
-              |....457..
-              |...1...3.
-              |..1....68
-              |..85...1.
-              |.9....4..""".stripMargin.replaceAll("\\n", "")
-
-// for measuring time
-def time_needed[T](i: Int, code: => T) = {
-  val start = System.nanoTime()
-  for (j <- 1 to i) code
-  val end = System.nanoTime()
-  s"${(end - start) / 1.0e9} secs"
-}
-
-
-println(time_needed(10, search(game2)))
-
-}
--- a/progs/trade.scala	Sat Mar 11 23:22:05 2023 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,56 +0,0 @@
-// Part 2 about Buy-Low-Sell-High using Yahoo Financial Data
-//===========================================================
-
-
-// (1) Complete the function that is given a list of floats
-// and calculuates the indices for when to buy the commodity 
-// and when to sell
-
-def trade_times(xs: List[Double]): (Int, Int) = ...
-
-
-// an example
-//val prices = List(28.0, 18.0, 20.0, 26.0, 24.0)
-//assert(trade_times(prices) == (1, 3), "the trade_times test fails")
-
-
-// (2) Complete the ``get webpage'' function that takes a
-// a stock symbol as argument and queries the Yahoo server
-// at
-//      http://ichart.yahoo.com/table.csv?s=<<insert stock symbol>>
-// 
-// This servive returns a CSV-list that needs to be separated into
-// a list of strings.
-
-def get_page(symbol: String): List[String] = ...
-
-// (3) Complete the function that processes the CSV list
-// extracting the dates and anjusted close prices. The
-// prices need to be transformed into Doubles.
-
-def process_page(symbol: String): List[(String, Double)] = ...
-
-
-// (4) Complete the query_company function that obtains the
-// processed CSV-list for a stock symbol. It should return
-// the dates for when to buy and sell the stocks of that company.
-
-def query_company(symbol: String): (String, String) =
-
-
-
-// some test cases
-
-//query_company("GOOG")
-
-// some more test cases
-/*
-val indices = List("GOOG", "AAPL", "MSFT", "IBM", "FB", "YHOO", "AMZN", "BIDU")
-
-for (name <- indices) {
-  val times = query_company(name)
-  println(s"Buy ${name} on ${times._1} and sell on ${times._2}")
-}
-*/
-
-