--- a/README Sun Dec 18 02:01:18 2016 +0000
+++ b/README Mon Dec 19 02:55:13 2016 +0000
@@ -16,4 +16,20 @@
LC_ALL=C sed -i.2 -- 's|def first_closed_tour_heuristic(dim: Int, path: Path): Option\[Path\] = \.\.\.|//def first_closed_tour_heuristic(dim: Int, path: Path): Option[Path] = ...|g' k*/knight3.scala.bak
-LC_ALL=C sed -i.2 -- 's|def first_tour_heuristic(dim: Int, path: Path): Option\[Path\] = \.\.\.|//def first_tour_heuristic(dim: Int, path: Path): Option[Path] = ...|g' k*/knight3.scala.bak
\ No newline at end of file
+LC_ALL=C sed -i.2 -- 's|def first_tour_heuristic(dim: Int, path: Path): Option\[Path\] = \.\.\.|//def first_tour_heuristic(dim: Int, path: Path): Option[Path] = ...|g' k*/knight3.scala.bak
+
+LC_ALL=C sed -i.2 -- 's|def nullable (r: Rexp) : Boolean = \.\.\.|//def nullable (r: Rexp) : Boolean = ...|g' k*/re.scala.bak
+
+LC_ALL=C sed -i.2 -- 's|def der (c: Char, r: Rexp) : Rexp = \.\.\.|//def der (c: Char, r: Rexp) : Rexp = ...|g' k*/re.scala.bak
+
+LC_ALL=C sed -i.2 -- 's|def simp(r: Rexp) : Rexp = \.\.\.|//def simp(r: Rexp) : Rexp = ...|g' k*/re.scala.bak
+
+LC_ALL=C sed -i.2 -- 's|def ders (s: List[Char], r: Rexp) : Rexp = \.\.\.|//def ders (s: List[Char], r: Rexp) : Rexp = ...|g' k*/re.scala.bak
+
+LC_ALL=C sed -i.2 -- 's|def matcher(r: Rexp, s: String): Boolean = \.\.\.|//def matcher(r: Rexp, s: String): Boolean = ...|g' k*/re.scala.bak
+
+LC_ALL=C sed -i.2 -- 's|def replace(r: Rexp, s1: String, s2: String): String = \.\.\.|//def replace(r: Rexp, s1: String, s2: String): String = ...|g' k*/re.scala.bak
+
+LC_ALL=C sed -i.2 -- 's|println(matcher(EVIL,|//println(matcher(EVIL,|g' k*/re.scala.bak
+
+LC_ALL=C sed -i.2 -- 's|for (i <- 1 to 5000001 by 500000)|for (i <- 1 to 11 by 10)|g' k*/re.scala.bak
\ No newline at end of file
--- a/marking/mark03a Sun Dec 18 02:01:18 2016 +0000
+++ b/marking/mark03a Mon Dec 19 02:55:13 2016 +0000
@@ -6,7 +6,7 @@
echo "" > $out
echo "Below is the feedback and provisional mark for your submission" >> $out
-echo "for CW 7, Part 2. Please note all marks are provisional until" >> $out
+echo "for CW 8, Part 1. Please note all marks are provisional until" >> $out
echo "ratified by the assessment board -- this is not an official" >> $out
echo "results transcript." >> $out
echo "" >> $out
@@ -30,15 +30,15 @@
}
-# marks for CW2b
+# marks for CW
marks=$(( 0 ))
-# knights3: var, comments test
+# var, return, ListBuffer test
#
-echo "knight3.scala does not contain vars, returns etc?" | tee -a $out
+echo "re.scala does not contain vars, returns etc?" | tee -a $out
-if (scala_vars knight3.scala)
+if (scala_vars re.scala)
then
echo " --> fail" | tee -a $out
tsts0=$(( 1 ))
@@ -51,14 +51,14 @@
# compilation test
if [ $tsts0 -eq 0 ]
then
- echo "knight3.scala runs?" | tee -a $out
+ echo "re.scala runs?" | tee -a $out
- if (scala_compile knight3.scala.bak)
+ if (scala_compile re.scala.bak)
then
echo " --> yes" | tee -a $out
tsts1=$(( 0 ))
else
- echo " --> scala knight3.scala did not run successfully" | tee -a $out
+ echo " --> scala re.scala did not run successfully" | tee -a $out
tsts1=$(( 1 ))
fi
else
@@ -67,11 +67,32 @@
if [ $tsts1 -eq 0 ]
then
- 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
+ echo " nullable(ZERO) == false" | tee -a $out
+ echo " nullable(ONE) == true" | tee -a $out
+ echo " nullable(CHAR('a')) == false" | tee -a $out
+ echo " nullable(ZERO | ONE) == true" | tee -a $out
+ echo " nullable(ZERO | CHAR('a')) == false" | tee -a $out
+ echo " nullable(ONE ~ ONE) == true" | tee -a $out
+ echo " nullable(ONE ~ CHAR('a')) == false" | tee -a $out
+ echo " nullable(STAR(ZERO)) == true" | tee -a $out
- if (scala_assert "knight3.scala.bak" "../../../marking/knight3a_test.scala")
+ if (scala_assert "re.scala.bak" "../../../marking/re1a_test.scala")
+ then
+ echo " --> success" | tee -a $out
+ marks=$(( marks + 1 ))
+ else
+ echo " --> test failed" | tee -a $out
+ fi
+fi
+
+if [ $tsts1 -eq 0 ]
+then
+ echo " der('a', ZERO | ONE) == (ZERO | ZERO)" | tee -a $out
+ echo " der('a', (CHAR('a') | ONE) ~ CHAR('a')) == ALT((ONE | ZERO) ~ CHAR('a'), ONE)" | tee -a $out
+ echo " der('a', STAR(CHAR('a'))) == (ONE ~ STAR(CHAR('a')))" | tee -a $out
+ echo " der('b', STAR(CHAR('a'))) == (ZERO ~ STAR(CHAR('a')))" | tee -a $out
+
+ if (scala_assert "re.scala.bak" "../../../marking/re1b_test.scala")
then
echo " --> success" | tee -a $out
marks=$(( marks + 1 ))
@@ -82,9 +103,13 @@
if [ $tsts1 -eq 0 ]
then
- echo " first_closed_tour_heuristic(6, List((3, 3))) found and ok?" | tee -a $out
+ echo " simp(ZERO | ONE) == ONE" | tee -a $out
+ echo " simp(STAR(ZERO | ONE)) == STAR(ZERO | ONE)" | tee -a $out
+ echo " simp(ONE ~ (ONE ~ (ONE ~ CHAR('a')))) == CHAR('a')" | tee -a $out
+ echo " simp(ONE ~ (ONE ~ (ONE ~ ZERO))) == ZERO" | tee -a $out
+ echo " simp(ALT(ONE ~ (ONE ~ (ONE ~ ZERO)), CHAR('a'))) == CHAR('a')" | tee -a $out
- if (scala_assert "knight3.scala.bak" "../../../marking/knight3b_test.scala")
+ if (scala_assert "re.scala.bak" "../../../marking/re1c_test.scala")
then
echo " --> success" | tee -a $out
marks=$(( marks + 1 ))
@@ -95,10 +120,19 @@
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
+ echo " EVIL = (a*)* b" | tee -a $out
+ echo " ders(List.fill(5)('a'),EVIL) == SEQ(SEQ(STAR(CHAR('a')),STAR(STAR(CHAR('a')))),CHAR('b'))" | tee -a $out
+ echo " ders(List('b'),EVIL) == ONE" | tee -a $out
+ echo " ders(List('b','b'),EVIL) == ZERO" | tee -a $out
+ echo " matcher(EVIL, \"a\" * 5 ++ \"b\") == true" | tee -a $out
+ echo " matcher(EVIL, \"b\") == true" | tee -a $out
+ echo " matcher(EVIL, \"bb\") == false" | tee -a $out
+ echo " matcher(\"abc\", \"abc\") == true" | tee -a $out
+ echo " matcher((\"ab\" | \"a\") ~ (ONE | \"bc\"), \"abc\") == true" | tee -a $out
+ echo " matcher(ONE, \"\") == true" | tee -a $out
+ echo " matcher(ZERO, \"\") == false" | tee -a $out
- if (scala_assert "knight3.scala.bak" "../../../marking/knight3c_test.scala")
+ if (scala_assert "re.scala.bak" "../../../marking/re1d_test.scala")
then
echo " --> success" | tee -a $out
marks=$(( marks + 1 ))
@@ -107,7 +141,21 @@
fi
fi
+if [ $tsts1 -eq 0 ]
+then
+ echo " replace(\"aa\".% | \"bb\", \"aabbbaaaaaaabaaaaabbaaaabb\" , \"c\") == \"ccbcabcaccc\"" | tee -a $out
+ echo " replace(\"aa\".% | \"bb\", \"abba\" , \"\") == \"aa\"" | tee -a $out
+
+ if (scala_assert "re.scala.bak" "../../../marking/re1e_test.scala")
+ then
+ echo " --> success" | tee -a $out
+ marks=$(( marks + 2 ))
+ else
+ echo " --> test failed" | tee -a $out
+ fi
+fi
+
## final marks
-echo "Overall mark for CW 7, Part 2" | tee -a $out
+echo "Overall mark for CW 8, Part 1" | tee -a $out
echo "$marks" | tee -a $out
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/marking/re1a_test.scala Mon Dec 19 02:55:13 2016 +0000
@@ -0,0 +1,20 @@
+
+import scala.concurrent._
+import scala.concurrent.duration._
+import ExecutionContext.Implicits.global
+import scala.language.postfixOps
+
+
+
+lazy val f = Future {
+ assert(nullable(ZERO) == false)
+ assert(nullable(ONE) == true)
+ assert(nullable(CHAR('a')) == false)
+ assert(nullable(ZERO | ONE) == true)
+ assert(nullable(ZERO | CHAR('a')) == false)
+ assert(nullable(ONE ~ ONE) == true)
+ assert(nullable(ONE ~ CHAR('a')) == false)
+ assert(nullable(STAR(ZERO)) == true)
+}
+
+Await.result(f, 120 second)
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/marking/re1b_test.scala Mon Dec 19 02:55:13 2016 +0000
@@ -0,0 +1,16 @@
+
+import scala.concurrent._
+import scala.concurrent.duration._
+import ExecutionContext.Implicits.global
+import scala.language.postfixOps
+
+
+
+lazy val f = Future {
+ 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)
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/progs/re_sol.scala Mon Dec 19 02:55:13 2016 +0000
@@ -0,0 +1,187 @@
+// 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")
+replace(r, "abb", "")
+
+replace("aa".% | "bb", "abba" , "")
+
+val r = SEQ("a",SEQ("b", "c"))
+val r = SEQ(SEQ("a", "b"), "c")
+
+val rm = der('a', r)
+der('b', r)
+der('c', r)
+
+der('a', rm)
+val rn = der('b', rm)
+der('c', rm)
+
+der('a', rn)
+der('b', rn)
+der('c', rn)
+
+// some testing data
+// the supposedly 'evil' regular expression (a*)* b
+
+val EVIL = SEQ(STAR(STAR(CHAR('a'))), CHAR('b'))
+
+ders(List.fill(5)('a') , EVIL)
+ders(List.fill(1)('b') , EVIL)
+matcher(EVIL, "a" * 15 ++ "b")
+matcher(EVIL, "b")
+println(matcher(EVIL, "a" * 1000 ++ "b"))
+println(matcher(EVIL, "a" * 1000))
+
+
+def time_needed[T](i: Int, code: => T) = {
+ val start = System.nanoTime()
+ for (j <- 1 to i) code
+ val end = System.nanoTime()
+ (end - start)/(i * 1.0e9)
+}
+
+for (i <- 1 to 5000001 by 500000) {
+ println(i + " " + "%.5f".format(time_needed(2, matcher(EVIL, "a" * i))))
+}
+
+
+