# HG changeset patch # User Christian Urban # Date 1478365907 0 # Node ID ab77f6006f1fdb62fcb182eb8949ca7c3ee7edd6 # Parent 7a5a29a32568d7063c5eb891bc2f1cf8b05fe3e1 updated diff -r 7a5a29a32568 -r ab77f6006f1f progs/knight2.scala --- a/progs/knight2.scala Thu Nov 03 01:05:20 2016 +0000 +++ b/progs/knight2.scala Sat Nov 05 17:11:47 2016 +0000 @@ -26,7 +26,7 @@ // non-circle tours def tour(n: Int)(steps: List[Pos]): List[List[Pos]] = { - if (steps.length == n * n) List(steps) + if (steps.length == n * n && moves(n)(steps.head).contains(steps.last)) List(steps) else (for (x <- moves(n)(steps.head).par; if (!steps.contains(x))) yield tour(n)(x :: steps)).toList.flatten } @@ -35,4 +35,5 @@ val n = 6 println(s"number simple tours: n = $n") -println((for (i <- 0 until n; j <- 0 until n) yield tour(n)(List((i, j)))).flatten.distinct.size) +println(tour(n)(List((1,1))).distinct.size) +//println((for (i <- 0 until n; j <- 0 until n) yield tour(n)(List((i, j)))).flatten.distinct.size) diff -r 7a5a29a32568 -r ab77f6006f1f progs/lecture1.scala --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/progs/lecture1.scala Sat Nov 05 17:11:47 2016 +0000 @@ -0,0 +1,9 @@ +// toList, toSet, toDouble +// function definition +// smart strings +// maps +// recursion +// webpages + + + diff -r 7a5a29a32568 -r ab77f6006f1f progs/lecture2.scala --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/progs/lecture2.scala Sat Nov 05 17:11:47 2016 +0000 @@ -0,0 +1,1 @@ +// sudoku diff -r 7a5a29a32568 -r ab77f6006f1f progs/lecture3.scala --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/progs/lecture3.scala Sat Nov 05 17:11:47 2016 +0000 @@ -0,0 +1,2 @@ +// regular expressions +// ?? polymorphism diff -r 7a5a29a32568 -r ab77f6006f1f progs/trade.scala --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/progs/trade.scala Sat Nov 05 17:11:47 2016 +0000 @@ -0,0 +1,66 @@ + + +val trades = List(28.0, 18.0, 20.0, 26.0, 24.0) + +def trade_times(xs: List[Double]): (Int, Int) = { + val low = xs.min + val low_index = xs.indexOf(low) + val rest = xs.drop(low_index) + val high = rest.max + val high_index = rest.indexOf(high) + (low_index, low_index + high_index) +} + + +trade_times(trades) + +assert(trade_times(List(10.0, 8.0, 7.0, 6.0)) == (3, 3), "the first test fails") + + + +import io.Source +import scala.util._ + +def get_page(url: String): List[String] = { + Try(Source.fromURL(url)("ISO-8859-1").getLines.toList). + getOrElse { println(s" Problem with: $url"); List() } +} + +def process_page(url: String): List[(String, Double)] = { + get_page(url).drop(1).map(_.split(",").toList).map((xs) => (xs(0), xs(6).toDouble)) +} + +def query_comp(name: String): (Int, Int) = { + val list = process_page("""http://ichart.yahoo.com/table.csv?s=""" + name) + trade_times(list.reverse.map(_._2)) +} + + +val indices = List("GOOG", "AAPL", "MSFT", "IBM", "FB", "YHOO", "AMZN", "BIDU") + +indices.map(query_comp(_)) + +val appl_page = get_page("""http://ichart.yahoo.com/table.csv?s=GOOG""") +val goog_page = get_page("""http://ichart.yahoo.com/table.csv?s=GOOG""") +goog_page.length + +val pairs = goog_page.drop(1).map(_.split(",").toList).map((xs:List[String]) => (xs(0), xs(6).toDouble)) + +pairs.map(_._2) + +trade_times(pairs.reverse.map(_._2)) + +pairs(3067) +pairs(11) + + +/* +scala trade.scala 2> /dev/null || echo "command1 borked it" + +command1 +if [ $? -ne 0 ]; then + echo "command1 borked it" +fi +*/ + +