Binary file cws/cw03.pdf has changed
--- a/cws/cw03.tex Thu Nov 17 16:21:56 2016 +0000
+++ b/cws/cw03.tex Fri Nov 18 18:47:02 2016 +0000
@@ -1,18 +1,31 @@
\documentclass{article}
-\usepackage{style}
+\usepackage{../style}
%%\usepackage{../langs}
\begin{document}
-\section*{Coursework 3}
+\section*{Coursework 8 (Scala, Regular Expressions}
-This coursework is worth XXX\% and is due on XXXX at
+This coursework is worth 10\% and is due on XXXX at
16:00. You are asked to implement a regular expression matcher.
+Make sure the files
+you submit can be processed by just calling \texttt{scala
+ <<filename.scala>>}.\bigskip
+
+\noindent
+\textbf{Important:} Do not use any mutable data structures in your
+submissions! They are not needed. This excluded the use of
+\texttt{ListBuffer}s, for example. Do not use \texttt{return} in your
+code! It has a different meaning in Scala, than in Java.
+Do not use \texttt{var}! This declares a mutable variable. Feel free to
+copy any code you need from files \texttt{knight1.scala},
+\texttt{knight2.scala} and \texttt{knight3.scala}. Make sure the
+functions you submit are defined on the ``top-level'' of Scala, not
+inside a class or object.
-
-\subsubsection*{Disclaimer}
+\subsection*{Disclaimer}
It should be understood that the work you submit represents
your own effort. You have not copied from anyone else. An
--- a/progs/drumb_sol.scala Thu Nov 17 16:21:56 2016 +0000
+++ b/progs/drumb_sol.scala Fri Nov 18 18:47:02 2016 +0000
@@ -42,7 +42,7 @@
// test case
-val p = get_prices(List("FB"), 2012 to 2014)
+val p_fb = get_prices(List("FB"), 2012 to 2014)
val p = get_prices(List("GOOG", "AAPL"), 2005 to 2012)
@@ -79,7 +79,7 @@
val somes_length = somes.length
if (somes_length == 0) balance
else {
- val portion: Double = (balance.toDouble) / somes_length
+ val portion: Double = balance.toDouble / somes_length.toDouble
balance + (for (x <- somes) yield (x * portion)).sum.toLong
}
}
@@ -115,3 +115,30 @@
println("Real data: " + investment(rstate_portfolio, 1978 to 2016, 100))
println("Blue data: " + investment(blchip_portfolio, 1978 to 2016, 100))
+val p_fb = get_prices(List("FB"), 2011 to 2016)
+
+// prices 2011 - 2016
+//
+List(List(None), List(None), List(Some(28.0)),
+ List(Some(54.709999)), List(Some(78.449997)),
+ List(Some(102.220001)))
+
+// deltas 2011 - 2016
+val d_fb = get_deltas(p_fb)
+
+List(List(None), List(None), List(Some(0.9539285357142858)),
+ List(Some(0.4339242996513305)), List(Some(0.30299560113431234)))
+
+yearly_yield(d_fb, 100, 0) //2011 => 100
+yearly_yield(d_fb, 100, 1) //2012 => 100
+yearly_yield(d_fb, 100, 2) //2013 => 195
+yearly_yield(d_fb, 195, 3) //2014 => 279
+yearly_yield(d_fb, 279, 4) //2015 => 363
+
+investment(List("FB"), 2011 to 2012, 100) // => 100
+investment(List("FB"), 2011 to 2013, 100) // => 100
+investment(List("FB"), 2011 to 2014, 100) // => 195
+investment(List("FB"), 2011 to 2015, 100) // => 279
+investment(List("FB"), 2011 to 2016, 100) // => 363
+
+
--- a/progs/re.scala Thu Nov 17 16:21:56 2016 +0000
+++ b/progs/re.scala Fri Nov 18 18:47:02 2016 +0000
@@ -6,8 +6,6 @@
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
-case class NTIMES(r: Rexp, n: Int) extends Rexp
-case class UPNTIMES(r: Rexp, n: Int) extends Rexp
// nullable function: tests whether the regular
// expression can recognise the empty string
@@ -18,8 +16,6 @@
case ALT(r1, r2) => nullable(r1) || nullable(r2)
case SEQ(r1, r2) => nullable(r1) && nullable(r2)
case STAR(_) => true
- case NTIMES(r, i) => if (i == 0) true else nullable(r)
- case UPNTIMES(r: Rexp, n: Int) => true
}
// derivative of a regular expression w.r.t. a character
@@ -32,13 +28,6 @@
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))
- case NTIMES(r1, i) =>
- if (i == 0) ZERO else
- if (nullable(r1)) SEQ(der(c, r1), UPNTIMES(r1, i - 1))
- else SEQ(der(c, r1), NTIMES(r1, i - 1))
- case UPNTIMES(r1, i) =>
- if (i == 0) ZERO
- else SEQ(der(c, r1), UPNTIMES(r1, i - 1))
}
def simp(r: Rexp) : Rexp = r match {
Binary file slides/slides02.pdf has changed
--- a/style.sty Thu Nov 17 16:21:56 2016 +0000
+++ b/style.sty Fri Nov 18 18:47:02 2016 +0000
@@ -9,6 +9,14 @@
\definecolor{darkblue}{rgb}{0,0,0.6}
\usepackage[colorlinks=true,urlcolor=darkblue,linkcolor=darkblue]{hyperref}
+\newcommand{\ZERO}{\mbox{\bf 0}}
+\newcommand{\ONE}{\mbox{\bf 1}}
+\newcommand{\Left}{\textit{Left}}
+\newcommand{\Der}{\textit{Der}}
+\newcommand{\der}{\textit{der}}
+\newcommand{\Ders}{\textit{Ders}}
+\newcommand{\ders}{\textit{ders}}
+
%%% for trees
%% http://anorien.csc.warwick.ac.uk/mirrors/CTAN/graphics/pgf/contrib/forest/forest.pdf