# HG changeset patch # User Christian Urban # Date 1511990549 0 # Node ID 863feeb5c760346aafee83cb4a2e5b91c5c09ad5 # Parent 92c31cbb1952ae8acbbed3dedd8d44fcb9718334 updated diff -r 92c31cbb1952 -r 863feeb5c760 testing1/drumb.scala --- a/testing1/drumb.scala Tue Nov 28 20:37:57 2017 +0000 +++ b/testing1/drumb.scala Wed Nov 29 21:22:29 2017 +0000 @@ -6,7 +6,7 @@ //two test portfolios -val blchip_portfolio = List("GOOG", "AAPL", "MSFT", "IBM", "FB", "AMZN", "BIDU", "FOOBAR") +val blchip_portfolio = List("GOOG", "AAPL", "MSFT", "IBM", "FB", "AMZN", "BIDU") val rstate_portfolio = List("PLD", "PSA", "AMT", "AIV", "AVB", "BXP", "CCI", "DLR", "EQIX", "EQR", "ESS", "EXR", "FRT", "GGP", "HCP") @@ -95,18 +95,18 @@ } } -yearly_yield(d, 100, 0) -compound_yield(d.take(6), 100, 0) +//yearly_yield(d, 100, 0) +//compound_yield(d.take(6), 100, 0) //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, 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, 100, 0) -yearly_yield(d, 125, 1) +//yearly_yield(d, 100, 0) +//yearly_yield(d, 125, 1) def investment(portfolio: List[String], years: Range, start_balance: Long): Long = { compound_yield(get_deltas(get_prices(portfolio, years)), start_balance, 0) @@ -123,14 +123,15 @@ //test cases for the two portfolios given above -println("Real data: " + investment(rstate_portfolio, 1978 to 1981, 100)) -println("Blue data: " + investment(blchip_portfolio, 1978 to 1981, 100)) +//println("Real data: " + investment(rstate_portfolio, 1978 to 1981, 100)) +//println("Blue data: " + investment(blchip_portfolio, 1978 to 1981, 100)) -for (i <- 1978 to 2017) { - println("Year " + i) - println("Real data: " + investment(rstate_portfolio, 1978 to i, 100)) - println("Blue data: " + investment(blchip_portfolio, 1978 to i, 100)) -} +//for (i <- 1978 to 2017) { +// println("Year " + i) +// println("Real data: " + investment(rstate_portfolio, 1978 to i, 100)) +// println("Blue data: " + investment(blchip_portfolio, 1978 to i, 100)) +//} + //1984 //1992 } diff -r 92c31cbb1952 -r 863feeb5c760 testing1/drumb_test.sh --- a/testing1/drumb_test.sh Tue Nov 28 20:37:57 2017 +0000 +++ b/testing1/drumb_test.sh Wed Nov 29 21:22:29 2017 +0000 @@ -17,7 +17,7 @@ # functional tests function scala_assert { - (ulimit -t 30 -m 1024000 ; scala -i "$1" "$2" -e "" 2> /dev/null 1> /dev/null) + (ulimit -t 30 -m 1024000 ; scala -i "$1" "$2" -e "") #2> /dev/null 1> /dev/null) } # purity test diff -r 92c31cbb1952 -r 863feeb5c760 testing2/knight3.scala --- a/testing2/knight3.scala Tue Nov 28 20:37:57 2017 +0000 +++ b/testing2/knight3.scala Wed Nov 29 21:22:29 2017 +0000 @@ -1,96 +1,45 @@ -// Part 3 about finding a single tour using the Warnsdorf Rule -//============================================================= - +import scala.annotation.tailrec object CW7c { - -type Pos = (Int, Int) -type Path = List[Pos] +type Pos = (Int, Int) // a position on a chessboard +type Path = List[Pos] // a path...a list of positions -def print_board(dim: Int, path: Path): Unit = { - println - for (i <- 0 until dim) { - for (j <- 0 until dim) { - print(f"${path.reverse.indexOf((i, j))}%3.0f ") - } - println - } +def is_legal(dim: Int, path: Path)(x: Pos) : Boolean = { + if((x._1 >= 0) && (x._2 >= 0) && (x._1 < dim) && (x._2 < dim)){ + !(path.contains(x)) + } else false + } + +def legal_moves(dim: Int, path: Path, x: Pos) : List[Pos] = { + val lst = List( (1,2),(2,1),(2,-1),(1,-2), (-1,-2),(-2,-1),(-2,1),(-1,2) ) + val mapping = lst.map(s => ( s._1 + x._1, s._2 + x._2) ) + for( i <- mapping if ( is_legal(dim,path)(i) )) yield i + } + +def ordered_moves(dim: Int, path: Path, x: Pos) : List[Pos] = { +legal_moves(dim,path,x).sortBy(legal_moves(dim,path,_).length ) } -def add_pair(x: Pos)(y: Pos): Pos = - (x._1 + y._1, x._2 + y._2) - -def is_legal(dim: Int, path: Path)(x: Pos): Boolean = - 0 <= x._1 && 0 <= x._2 && x._1 < dim && x._2 < dim && !path.contains(x) - -def moves(x: Pos): List[Pos] = - List(( 1, 2),( 2, 1),( 2, -1),( 1, -2), - (-1, -2),(-2, -1),(-2, 1),(-1, 2)).map(add_pair(x)) - -def legal_moves(dim: Int, path: Path, x: Pos): List[Pos] = - moves(x).filter(is_legal(dim, path)) +def first(xs: List[Pos], f: Pos => Option[Path]) : Option[Path] ={ + if(xs.isEmpty) + None + else { + val b = f(xs.head) + if (b!=None) + b + else + first(xs.tail,f) + } + } -def ordered_moves(dim: Int, path: Path, x: Pos): List[Pos] = - legal_moves(dim, path, x).sortBy((x) => legal_moves(dim, path, x).length) - - -import scala.annotation.tailrec - -@tailrec -def first(xs: List[Pos], f: Pos => Option[Path]): Option[Path] = xs match { - case Nil => None - case x::xs => { - val result = f(x) - if (result.isDefined) result else first(xs, f) +def first_closed_tour_heuristic(dim: Int, path: Path) : Option[Path] = { + if (dim < 5) None + else + if(path.length==dim*dim) Some(path) + else + first(ordered_moves(dim,path,path.head),y => first_closed_tour_heuristic(dim, y::path)) } + } -def first_closed_tour_heuristic(dim: Int, path: Path): Option[Path] = { - if (path.length == dim * dim && moves(path.head).contains(path.last)) Some(path) - else - first(ordered_moves(dim, path, path.head), (x: Pos) => first_closed_tour_heuristic(dim, x::path)) -} - -/* -for (dim <- 1 to 6) { - val t = first_closed_tour_heuristic(dim, List((dim / 2, dim / 2))) - println(s"${dim} x ${dim} closed: " + (if (t == None) "" else { print_board(dim, t.get) ; "" })) -}*/ - - -def first_tour_heuristic(dim: Int, path: Path): Option[Path] = { - - @tailrec - def aux(dim: Int, path: Path, moves: List[Pos]): Option[Path] = - if (path.length == dim * dim) Some(path) - else - moves match { - case Nil => None - case x::xs => { - val r = first_tour_heuristic(dim, x::path) - if (r.isDefined) r else aux(dim, path, xs) - } - } - - aux(dim, path, ordered_moves(dim, path, path.head)) -} - -/* -def first_tour_heuristic(dim: Int, path: Path): Option[Path] = { - if (path.length == dim * dim) Some(path) - else - first(ordered_moves(dim, path, path.head), (x: Pos) => first_tour_heuristic(dim, x::path)) -} -*/ - -/* -for (dim <- 1 to 50) { - val t = first_tour_heuristic(dim, List((dim / 2, dim / 2))) - println(s"${dim} x ${dim}: " + (if (t == None) "" else { print_board(dim, t.get) ; "" })) -} -*/ - -} - - -//CW7c.first_tour_heuristic(50, List((0,0))).get +first_closed_tour_heuristic(6, List((3, 3))) diff -r 92c31cbb1952 -r 863feeb5c760 testing3/bf.scala --- a/testing3/bf.scala Tue Nov 28 20:37:57 2017 +0000 +++ b/testing3/bf.scala Wed Nov 29 21:22:29 2017 +0000 @@ -142,7 +142,6 @@ >>>+[[-]++++++>>>]<<<[[<++++++++<++>>-]+<.<[>----<-]<] <<[>>>>>[>>>[-]+++++++++<[>-<-]+++++++++>[-[<->-]+[<<<]]<[>+<-]>]<<-]<<-]""", Map()) - //collatz numbers (need to be typed in) start(""">,[[----------[ >>>[>>>>]+[[-]+<[->>>>++>>>>+[>>>>]++[->+<<<<<]]<<<] diff -r 92c31cbb1952 -r 863feeb5c760 testing3/bf1a_test.scala --- a/testing3/bf1a_test.scala Tue Nov 28 20:37:57 2017 +0000 +++ b/testing3/bf1a_test.scala Wed Nov 29 21:22:29 2017 +0000 @@ -1,17 +1,17 @@ -import scala.concurrent._ -import scala.concurrent.duration._ -import ExecutionContext.Implicits.global -import scala.language.postfixOps -import scala.language.reflectiveCalls +//import scala.concurrent._ +//import scala.concurrent.duration._ +//import ExecutionContext.Implicits.global +//import scala.language.postfixOps +//import scala.language.reflectiveCalls -lazy val f = Future { +//lazy val f = Future { import CW8b._ assert(sread(Map(), 2) == 0) assert(sread(Map(2 -> 1), 2) == 1) assert(write(Map(), 1, 2) == Map(1 -> 2)) assert(write(Map(1 -> 0), 1, 2) == Map(1 -> 2)) -} +//} -Await.result(f, 120 second) +//Await.result(f, 120 second) diff -r 92c31cbb1952 -r 863feeb5c760 testing3/bf1b_test.scala --- a/testing3/bf1b_test.scala Tue Nov 28 20:37:57 2017 +0000 +++ b/testing3/bf1b_test.scala Wed Nov 29 21:22:29 2017 +0000 @@ -1,11 +1,11 @@ -import scala.concurrent._ -import scala.concurrent.duration._ -import ExecutionContext.Implicits.global -import scala.language.postfixOps -import scala.language.reflectiveCalls +//import scala.concurrent._ +//import scala.concurrent.duration._ +//import ExecutionContext.Implicits.global +//import scala.language.postfixOps +//import scala.language.reflectiveCalls -lazy val f = Future { +//lazy val f = Future { import CW8b._ assert(jumpRight("[******]***", 1, 0) == 8) @@ -16,6 +16,6 @@ assert(jumpLeft("[******]***", 6, 0) == 1) assert(jumpLeft("[******]***", 7, 0) == -1) assert(jumpLeft("[*[][]*]***", 6, 0) == 1) -} +//} -Await.result(f, 120 second) +//Await.result(f, 120 second) diff -r 92c31cbb1952 -r 863feeb5c760 testing3/bf1c_test.scala --- a/testing3/bf1c_test.scala Tue Nov 28 20:37:57 2017 +0000 +++ b/testing3/bf1c_test.scala Wed Nov 29 21:22:29 2017 +0000 @@ -1,11 +1,11 @@ -import scala.concurrent._ -import scala.concurrent.duration._ -import ExecutionContext.Implicits.global -import scala.language.postfixOps -import scala.language.reflectiveCalls +//import scala.concurrent._ +//import scala.concurrent.duration._ +//import ExecutionContext.Implicits.global +//import scala.language.postfixOps +//import scala.language.reflectiveCalls -lazy val f = Future { +//lazy val f = Future { import CW8b._ assert(start("[-]", Map(0 -> 100)) == Map(0 -> 0)) @@ -14,6 +14,6 @@ assert(start("""++++++++[>++++[>++>+++>+++>+<<<<-]>+>+>->>+[<] <-]>>.>---.+++++++..+++.>>.<-.<.+++.------.--------.>>+.>++.""", Map()) == Map(0 -> 0, 5 -> 33, 1 -> 0, 6 -> 10, 2 -> 72, 3 -> 100, 4 -> 87)) -} +//} -Await.result(f, 120 second) +//Await.result(f, 120 second) diff -r 92c31cbb1952 -r 863feeb5c760 testing3/re1a_test.scala --- a/testing3/re1a_test.scala Tue Nov 28 20:37:57 2017 +0000 +++ b/testing3/re1a_test.scala Wed Nov 29 21:22:29 2017 +0000 @@ -1,11 +1,11 @@ -import scala.concurrent._ -import scala.concurrent.duration._ -import ExecutionContext.Implicits.global -import scala.language.postfixOps -import scala.language.reflectiveCalls +//import scala.concurrent._ +//import scala.concurrent.duration._ +//import ExecutionContext.Implicits.global +//import scala.language.postfixOps +//import scala.language.reflectiveCalls -lazy val f = Future { +//lazy val f = Future { import CW8a._ assert(nullable(ZERO) == false) @@ -16,6 +16,6 @@ assert(nullable(ONE ~ ONE) == true) assert(nullable(ONE ~ CHAR('a')) == false) assert(nullable(STAR(ZERO)) == true) -} +//} -Await.result(f, 120 second) +//Await.result(f, 120 second) diff -r 92c31cbb1952 -r 863feeb5c760 testing3/re1b_test.scala --- a/testing3/re1b_test.scala Tue Nov 28 20:37:57 2017 +0000 +++ b/testing3/re1b_test.scala Wed Nov 29 21:22:29 2017 +0000 @@ -1,18 +1,18 @@ -import scala.concurrent._ -import scala.concurrent.duration._ -import ExecutionContext.Implicits.global -import scala.language.postfixOps -import scala.language.reflectiveCalls +//import scala.concurrent._ +//import scala.concurrent.duration._ +//import ExecutionContext.Implicits.global +//import scala.language.postfixOps +//import scala.language.reflectiveCalls -lazy val f = Future { +//lazy val f = Future { import CW8a._ assert(der('a', ZERO | ONE) == (ZERO | ZERO)) assert(der('a', (CHAR('a') | ONE) ~ CHAR('a')) == ALT((ONE | ZERO) ~ CHAR('a'), ONE)) assert(der('a', STAR(CHAR('a'))) == (ONE ~ STAR(CHAR('a')))) assert(der('b', STAR(CHAR('a'))) == (ZERO ~ STAR(CHAR('a')))) -} +//} -Await.result(f, 120 second) +//Await.result(f, 120 second) diff -r 92c31cbb1952 -r 863feeb5c760 testing3/re1c_test.scala --- a/testing3/re1c_test.scala Tue Nov 28 20:37:57 2017 +0000 +++ b/testing3/re1c_test.scala Wed Nov 29 21:22:29 2017 +0000 @@ -1,12 +1,12 @@ -import scala.concurrent._ -import scala.concurrent.duration._ -import ExecutionContext.Implicits.global -import scala.language.postfixOps -import scala.language.reflectiveCalls +//import scala.concurrent._ +//import scala.concurrent.duration._ +//import ExecutionContext.Implicits.global +//import scala.language.postfixOps +//import scala.language.reflectiveCalls -lazy val f = Future { +//lazy val f = Future { import CW8a._ assert(simp(ZERO | ONE) == ONE) @@ -16,6 +16,9 @@ assert(simp(ALT(ONE ~ (ONE ~ (ONE ~ ZERO)), CHAR('a'))) == CHAR('a')) assert(simp(CHAR('a') | CHAR('a')) == CHAR('a')) assert(simp(ONE | CHAR('a')) == (ONE | CHAR('a'))) -} + assert(simp(ALT((CHAR('a') | ZERO) ~ ONE, + ((ONE | CHAR('b')) | CHAR('c')) ~ (CHAR('d') ~ ZERO))) == CHAR('a')) -Await.result(f, 30 second) +//} + +//Await.result(f, 30 second) diff -r 92c31cbb1952 -r 863feeb5c760 testing3/re1d_test.scala --- a/testing3/re1d_test.scala Tue Nov 28 20:37:57 2017 +0000 +++ b/testing3/re1d_test.scala Wed Nov 29 21:22:29 2017 +0000 @@ -1,13 +1,13 @@ -import scala.concurrent._ -import scala.concurrent.duration._ -import ExecutionContext.Implicits.global -import scala.language.postfixOps -import scala.language.reflectiveCalls +//import scala.concurrent._ +//import scala.concurrent.duration._ +//import ExecutionContext.Implicits.global +//import scala.language.postfixOps +//import scala.language.reflectiveCalls -lazy val f = Future { +//lazy val f = Future { import CW8a._ val EVIL_urban = SEQ(STAR(STAR(CHAR('a'))), CHAR('b')) @@ -36,6 +36,6 @@ assert(matcher(ONE | CHAR('a'), "") == true) //println("12") assert(matcher(ONE | CHAR('a'), "a") == true) -} +//} -Await.result(f, 90 second) +//Await.result(f, 90 second) diff -r 92c31cbb1952 -r 863feeb5c760 testing3/re1e_test.scala --- a/testing3/re1e_test.scala Tue Nov 28 20:37:57 2017 +0000 +++ b/testing3/re1e_test.scala Wed Nov 29 21:22:29 2017 +0000 @@ -1,11 +1,11 @@ -import scala.concurrent._ -import scala.concurrent.duration._ -import ExecutionContext.Implicits.global -import scala.language.postfixOps -import scala.language.reflectiveCalls +//import scala.concurrent._ +//import scala.concurrent.duration._ +//import ExecutionContext.Implicits.global +//import scala.language.postfixOps +//import scala.language.reflectiveCalls -lazy val f = Future { +//lazy val f = Future { import CW8a._ val EVIL_urban = SEQ(STAR(STAR(CHAR('a'))), CHAR('b')) @@ -14,6 +14,6 @@ assert(size(der('a', der('a', der('a', EVIL_urban)))) == 58) assert(size(ders("aaaaaa".toList, EVIL_urban)) == 8) -} +//} -Await.result(f, 120 second) +//Await.result(f, 120 second) diff -r 92c31cbb1952 -r 863feeb5c760 testing3/re_test.sh --- a/testing3/re_test.sh Tue Nov 28 20:37:57 2017 +0000 +++ b/testing3/re_test.sh Wed Nov 29 21:22:29 2017 +0000 @@ -108,7 +108,8 @@ echo " simp(ALT(ONE ~ (ONE ~ (ONE ~ ZERO)), CHAR('a'))) == CHAR('a')" >> $out echo " simp(CHAR('a') | CHAR('a')) == CHAR('a')" >> $out echo " simp(ONE | CHAR('a')) == (ONE | CHAR('a'))" >> $out - + echo " simp(ALT((CHAR('a') | ZERO) ~ ONE," >> $out + echo " ((ONE | CHAR('b')) | CHAR('c')) ~ (CHAR('d') ~ ZERO))) == CHAR('a')" >> $out if (scala_assert "re.scala" "re1c_test.scala") then echo " --> success" >> $out @@ -120,7 +121,7 @@ if [ $tsts1 -eq 0 ] then - echo " let EVIL = (a*)* b" >> $out + echo " val EVIL = SEQ(STAR(STAR(CHAR('a'))), CHAR('b'))" >> $out echo " ders(List.fill(5)('a'),EVIL) == SEQ(SEQ(STAR(CHAR('a')),STAR(STAR(CHAR('a')))),CHAR('b'))" >> $out echo " ders(List('b'),EVIL) == ONE" >> $out echo " ders(List('b','b'),EVIL) == ZERO" >> $out @@ -145,7 +146,7 @@ if [ $tsts1 -eq 0 ] then - echo " let EVIL = (a*)* b" >> $out + echo " val EVIL = SEQ(STAR(STAR(CHAR('a'))), CHAR('b'))" >> $out echo " size(der('a', der('a', EVIL))) == 28" >> $out echo " size(der('a', der('a', der('a', EVIL)))) == 58" >> $out echo " size(ders(\"aaaaaa\".toList, EVIL)) == 8" >> $out