# HG changeset patch # User Christian Urban # Date 1485307504 0 # Node ID 350d4364f6f93b8457e2c5b47525ff927a9f0d59 # Parent 085bf16963c6699050603320c3a2a869611691f6# Parent 93260be6770f579aa9fb437eff246e8354c2eea6 updated diff -r 93260be6770f -r 350d4364f6f9 marking/mark02 --- a/marking/mark02 Tue Jan 24 12:57:40 2017 +0000 +++ b/marking/mark02 Wed Jan 25 01:25:04 2017 +0000 @@ -12,7 +12,7 @@ echo "" >> $out function scala_vars { - (egrep '\bvar\b|\breturn\b|ListBuffer|mutable' "$1" 2> /dev/null 1> /dev/null) + (egrep 'var|return|ListBuffer|mutable' "$1" 2> /dev/null 1> /dev/null) } @@ -189,6 +189,38 @@ fi +# knights3: var, comments test +# +#echo "knight3.scala does not contain vars, returns etc?" | tee -a $out + +#if (scala_vars knight3.scala) +#then +# echo " --> fail" | tee -a $out +# tsts0=$(( 1 )) +#else +# echo " --> success" | tee -a $out +# tsts0=$(( 0 )) +#fi + + +# compilation test +#if [ $tsts0 -eq 0 ] +#then +# echo "knight3.scala runs?" | tee -a $out +# +# if (scala_compile knight3.scala.bak) +# then +# echo " --> success" | tee -a $out +# tsts1=$(( 0 )) +# else +# echo " --> scala did not run knight3.scala" | tee -a $out +# tsts1=$(( 1 )) +# fi +#else +# tsts1=$(( 1 )) +#fi + + ## final marks -echo "Overall mark for CW 7, Part 1 " | tee -a $out +echo "Overall mark for CW 2, Part 1 " | tee -a $out echo "$marks" | tee -a $out diff -r 93260be6770f -r 350d4364f6f9 marking/mark02b --- a/marking/mark02b Tue Jan 24 12:57:40 2017 +0000 +++ b/marking/mark02b Wed Jan 25 01:25:04 2017 +0000 @@ -26,7 +26,7 @@ # functional tests function scala_assert { - (scala -i "$1" "$2" -e "" 2> /dev/null 1> /dev/null) + (scala -i "$1" "$2" -e "") # 2> /dev/null 1> /dev/null) } @@ -65,12 +65,11 @@ tsts1=$(( 1 )) fi - if [ $tsts1 -eq 0 ] then - echo " ordered_moves(8, List((3,4), (3,2)), (1,3)) == (0,1), (0,5), (2,1), (2,5)" | tee -a $out - echo " ordered_moves(8, List((4,0)), (0,0)) == (2,1), (1,2)" | tee -a $out - echo " ordered_moves(8, List((0,4)), (0,0)) == (1,2), (2,1)" | tee -a $out + echo " ordered_moves(8, List((3,4), (3,2)), (1, 3)) == List((0,1), (0,5), (2,1), (2,5))" | tee -a $out + echo " ordered_moves(8, List((4,0)), (0,0)) == List((2,1), (1,2))" | tee -a $out + echo " ordered_moves(8, List((0,4)), (0,0)) == List((1,2), (2,1))" | tee -a $out if (scala_assert "knight3.scala.bak" "../../../marking/knight3a_test.scala") then @@ -83,7 +82,7 @@ if [ $tsts1 -eq 0 ] then - echo " first_closed_tour_heuristic(6, List((3, 3))) is ok?" | tee -a $out + echo " first_closed_tour_heuristic(6, List((3, 3))) found and ok?" | tee -a $out if (scala_assert "knight3.scala.bak" "../../../marking/knight3b_test.scala") then @@ -94,6 +93,20 @@ fi fi +if [ $tsts1 -eq 0 ] +then + echo " first_tour_heuristic(8, List((0,0))) found and ok?" | tee -a $out + echo " first_tour_heuristic(50, List((0,0))) found and ok?" | tee -a $out + + if (scala_assert "knight3.scala.bak" "../../../marking/knight3c_test.scala") + then + echo " --> success" | tee -a $out + marks=$(( marks + 1 )) + else + echo " --> test failed" | tee -a $out + fi +fi + ## final marks echo "Overall mark for CW 7, Part 2" | tee -a $out diff -r 93260be6770f -r 350d4364f6f9 progs/re2_sol.scala --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/progs/re2_sol.scala Wed Jan 25 01:25:04 2017 +0000 @@ -0,0 +1,215 @@ +// Part 1 about Regular Expression Matching +//========================================== + +abstract class Rexp +case object ZERO extends Rexp +case object ONE extends Rexp +case class CHAR(c: Char) extends Rexp +case class ALT(r1: Rexp, r2: Rexp) extends Rexp +case class SEQ(r1: Rexp, r2: Rexp) extends Rexp +case class STAR(r: Rexp) extends Rexp + +// some convenience for typing in regular expressions + +import scala.language.implicitConversions +import scala.language.reflectiveCalls + +def charlist2rexp(s: List[Char]): Rexp = s match { + case Nil => ONE + case c::Nil => CHAR(c) + case c::s => SEQ(CHAR(c), charlist2rexp(s)) +} +implicit def string2rexp(s: String): Rexp = charlist2rexp(s.toList) + +implicit def RexpOps (r: Rexp) = new { + def | (s: Rexp) = ALT(r, s) + def % = STAR(r) + def ~ (s: Rexp) = SEQ(r, s) +} + +implicit def stringOps (s: String) = new { + def | (r: Rexp) = ALT(s, r) + def | (r: String) = ALT(s, r) + def % = STAR(s) + def ~ (r: Rexp) = SEQ(s, r) + def ~ (r: String) = SEQ(s, r) +} + +// (1a) Complete the function nullable according to +// the definition given in the coursework; this +// function checks whether a regular expression +// can match the empty string + +def nullable (r: Rexp) : Boolean = r match { + case ZERO => false + case ONE => true + case CHAR(_) => false + case ALT(r1, r2) => nullable(r1) || nullable(r2) + case SEQ(r1, r2) => nullable(r1) && nullable(r2) + case STAR(_) => true +} + +// (1b) Complete the function der according to +// the definition given in the coursework; this +// function calculates the derivative of a +// regular expression w.r.t. a character + +def der (c: Char, r: Rexp) : Rexp = r match { + case ZERO => ZERO + case ONE => ZERO + case CHAR(d) => if (c == d) ONE else ZERO + case ALT(r1, r2) => ALT(der(c, r1), der(c, r2)) + case SEQ(r1, r2) => + if (nullable(r1)) ALT(SEQ(der(c, r1), r2), der(c, r2)) + else SEQ(der(c, r1), r2) + case STAR(r1) => SEQ(der(c, r1), STAR(r1)) +} + +// (1c) Complete the function der according to +// the specification given in the coursework; this +// function simplifies a regular expression; +// however it does not simplify inside STAR-regular +// expressions + +def simp(r: Rexp) : Rexp = r match { + case ALT(r1, r2) => (simp(r1), simp(r2)) match { + case (ZERO, r2s) => r2s + case (r1s, ZERO) => r1s + case (r1s, r2s) => if (r1s == r2s) r1s else ALT (r1s, r2s) + } + case SEQ(r1, r2) => (simp(r1), simp(r2)) match { + case (ZERO, _) => ZERO + case (_, ZERO) => ZERO + case (ONE, r2s) => r2s + case (r1s, ONE) => r1s + case (r1s, r2s) => SEQ(r1s, r2s) + } + case r => r +} + +// (1d) Complete the two functions below; the first +// calculates the derivative w.r.t. a string; the second +// is the regular expression matcher taking a regular +// expression and a string and checks whether the +// string matches the regular expression + +def ders (s: List[Char], r: Rexp) : Rexp = s match { + case Nil => r + case c::s => ders(s, simp(der(c, r))) +} + +// main matcher function +def matcher(r: Rexp, s: String): Boolean = nullable(ders(s.toList, r)) + + +// (1e) Complete the function below: it searches (from the left to +// right) in string s1 all the non-empty substrings that match the +// regular expression -- these substrings are assumed to be +// the longest substrings matched by the regular expression and +// assumed to be non-overlapping. All these substrings in s1 are replaced +// by s2. + + + +def splits(s: String): List[(String, String)] = + (for (i <- (1 to s.length).toList) yield s.splitAt(i)).reverse + +splits("abcde") +splits("") + +def first(r: Rexp, lst: List[(String, String)]): Option[String] = lst match { + case Nil => None + case (s1, s2)::xs => if (matcher(r, s1)) Some(s2) else first(r, xs) +} + +"abcd".head + +def replace(r: Rexp, s1: String, s2: String): String = first(r, splits(s1)) match { + case None if (s1 == "") => "" + case None => s1.head.toString ++ replace(r, s1.tail, s2) + case Some(s) => s2 ++ replace(r, s, s2) +} + +val s1 = "aabbbaaaaaaabaaaaabbaaaabb" +val r: Rexp = "aa".% | "bb" +splits(s1) +first(r, splits(s1)) + +replace(r, s1, "c") + +splits("bb") +first(r, splits("bb")) +replace(r, "abb", "c") + + +// PART 2 +//======== + + +// (2a) + +import scala.annotation.tailrec + +@tailrec +def iterT[A](n: Int, f: A => A, x: A): A = + if (n == 0) x else iterT(n - 1, f, f(x)) + + +//non-tail recursive iter + +def iter[A](n: Int, f: A => A, x: A): A = + if (n == 0) x else f(iter(n - 1,f, x)) + + +iter(200000, (x: Int) => x + 1, 0) +iterT(200000, (x: Int) => x + 1, 0) +iterT(100, (x: Int) => x * 2, 2) +iterT(100, (x: BigInt) => x * 2, BigInt(2)) +iterT(10, (x: String) => x ++ "a", "a") + +// (2b) + +def size(r: Rexp): Int = r match { + case ZERO => 1 + case ONE => 1 + case CHAR(_) => 1 + case ALT(r1, r2) => 1 + size(r1) + size (r2) + case SEQ(r1, r2) => 1 + size(r1) + size (r2) + case STAR(r1) => 1 + size(r1) +} + + +val EVIL = SEQ(STAR(STAR(CHAR('a'))), CHAR('b')) +size(iterT(20, (r: Rexp) => der('a', r), EVIL)) // should produce 7340068 +size(iterT(20, (r: Rexp) => simp(der('a', r)), EVIL)) // should produce 8 + + +// (2c) + +@tailrec +def fixpT[A](f: A => A, x: A): A = { + val fx = f(x) + if (fx == x) x else fixpT(f, fx) +} + +fixpT((x:Int) => if (200000 < x) x else x + 1, 0) + +def ctest(n: Long): Long = + if (n == 1) 1 else + if (n % 2 == 0) n / 2 else 3 * n + 1 + +fixpT(ctest, 97L) +fixpT(ctest, 871L) +fixpT(ctest, 77031L) +fixpT(ctest, 837799L) + +def foo(s: String): String = { + if (matcher("a", s)) "a" else + if (matcher("aa" ~ STAR("aa"), s)) s.take(s.length / 2) + else "a" ++ s * 3 +} + +fixpT(foo, "a" * 97) +fixpT(foo, "a" * 871) + +