# HG changeset patch # User Christian Urban <christian.urban@kcl.ac.uk> # Date 1668684890 0 # Node ID b73e7ce91c10fe58f112c9cc121192824ad3d420 # Parent 7a0735db47881971dafdb288e3e3f13095c9bceb updated diff -r 7a0735db4788 -r b73e7ce91c10 cws/core_cw01.pdf Binary file cws/core_cw01.pdf has changed diff -r 7a0735db4788 -r b73e7ce91c10 cws/core_cw02.pdf Binary file cws/core_cw02.pdf has changed diff -r 7a0735db4788 -r b73e7ce91c10 cws/core_cw03.pdf Binary file cws/core_cw03.pdf has changed diff -r 7a0735db4788 -r b73e7ce91c10 cws/main_cw01.pdf Binary file cws/main_cw01.pdf has changed diff -r 7a0735db4788 -r b73e7ce91c10 cws/main_cw02.pdf Binary file cws/main_cw02.pdf has changed diff -r 7a0735db4788 -r b73e7ce91c10 cws/main_cw02.tex --- a/cws/main_cw02.tex Mon Nov 14 12:04:21 2022 +0000 +++ b/cws/main_cw02.tex Thu Nov 17 11:34:50 2022 +0000 @@ -213,7 +213,28 @@ where \pcode{secrets} is the list generated under Task 1. In all cases above the iscore of the resulting secrets is 0, but this does not need to be the case -in general.\\ +in general. + +\color{red} + Note that the template gives as type for \texttt{evil}: + + \begin{center} + \texttt{def evil(secrets: List[String], word: String) = ???} + \end{center} + + where the return type is left unspecified. This return type is not needed when + functions are not recursive---\texttt{evil} is meant to be just a wrapper that + calls \texttt{lowest} with appropriate default arguments and returns whatever + \texttt{lowest} returns. Therefore a return type is not needed. But a slightly + more accurate template definition for \texttt{evil} is:\medskip\medskip + + \begin{minipage}{1.05\textwidth} + \begin{center} + \texttt{def evil(secrets: List[String], word: String) : List[String] = ???} + \end{center} + \end{minipage}\medskip\medskip + + where also the return type is explicitly given.\\\color{black} \mbox{}\hfill [1.5 Marks] \item[(6)] The secrets generated in Task 5 are the ones with the lowest score @@ -249,6 +270,14 @@ rank(frequencies(secrets), "fuzzy") => 4.898735738513722 \end{lstlisting} + \color{red} + The return type for \texttt{rank} is \texttt{Double}: + + \begin{center} + \texttt{def rank(frqs: Map[Char, Double], s: String) : Double = ???} + \end{center} + \color{black} + Finally, implement a function \pcode{ranked_evil} that selects from the output of \pcode{evil} the string(s) which are highest ranked in evilness. @@ -263,6 +292,14 @@ This means if the user types in "abbey" then the most evil word to choose as secret is ``whizz'' (according to our calculations). This word has a zero \pcode{iscore} and the most obscure letters. +\color{red} + The return type for \texttt{ranked\_evil} is \texttt{List[String]}: + + \begin{center} + \texttt{def ranked\_evil(secrets: List[String], word: String) : List[String] = ???} + \end{center} + \color{black} + % %\color{red} %\section*{Correction with \texttt{ranked\_evil}} diff -r 7a0735db4788 -r b73e7ce91c10 cws/main_cw03.pdf Binary file cws/main_cw03.pdf has changed diff -r 7a0735db4788 -r b73e7ce91c10 cws/main_cw04.pdf Binary file cws/main_cw04.pdf has changed diff -r 7a0735db4788 -r b73e7ce91c10 cws/main_cw05.pdf Binary file cws/main_cw05.pdf has changed diff -r 7a0735db4788 -r b73e7ce91c10 main_solution3/re.scala --- a/main_solution3/re.scala Mon Nov 14 12:04:21 2022 +0000 +++ b/main_solution3/re.scala Thu Nov 17 11:34:50 2022 +0000 @@ -113,7 +113,6 @@ //println(simp(((CHAR('a') | ZERO) ~ ONE) | // (((ONE | CHAR('b')) | CHAR('c')) ~ (CHAR('d') ~ ZERO)))) - // (7) def ders (s: List[Char], r: Rexp) : Rexp = s match { @@ -191,3 +190,12 @@ } + + + + +/* +// if nullable(r1) + ALTs(SEQs(der(c, r1)::rs)::(rs filter what is nullable) .map(der(c,_))) + +*/ \ No newline at end of file diff -r 7a0735db4788 -r b73e7ce91c10 main_testing3/re_test.sh --- a/main_testing3/re_test.sh Mon Nov 14 12:04:21 2022 +0000 +++ b/main_testing3/re_test.sh Thu Nov 17 11:34:50 2022 +0000 @@ -200,7 +200,7 @@ if [ $tsts -eq 0 ] then echo -e " val EVIL = SEQ(STAR(STAR(CHAR('a'))), CHAR('b'))" >> $out - echo -e " ders(\"aaaaa\".toList, EVIL) == SEQ(SEQ(STAR(CHAR('a')),STAR(STAR(CHAR('a')))),CHAR('b'))" >> $out + echo -e " ders(\"aaaaa\".toList, EVIL) == SEQs(List(STAR(CHAR('a')), STAR(STAR(CHAR('a'))), CHAR('b')))" >> $out echo -e " ders(List('b'), EVIL) == ONE" >> $out echo -e " ders(\"bb\".toList, EVIL) == ZERO" >> $out echo -e " matcher(EVIL, \"a\" * 5 ++ \"b\") == true" >> $out diff -r 7a0735db4788 -r b73e7ce91c10 wsheets/wsh02.tex --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/wsheets/wsh02.tex Thu Nov 17 11:34:50 2022 +0000 @@ -0,0 +1,110 @@ +% !TEX program = xelatex +\documentclass{article} +\usepackage{../styles/style} +\usepackage{../styles/langs} +\usepackage{tikz} +\usepackage{pgf} +\usepackage{marvosym} +\usepackage{boxedminipage} + +\lstset{escapeinside={/*!}{!*/}} +\newcommand{\annotation}[1]{\hfill\footnotesize{}#1} + +\usepackage{menukeys} + + +% Exact colors from NB +\usepackage[breakable]{tcolorbox} +\definecolor{incolor}{HTML}{303F9F} +\definecolor{outcolor}{HTML}{D84315} +\definecolor{cellborder}{HTML}{CFCFCF} +\definecolor{cellbackground}{HTML}{F7F7F7} + + + +\begin{document} +\fnote{\copyright{} Christian Urban, King's College London, 2022} + +\section*{Scala Worksheet 2} + + + +\subsection*{Task 1 (Options)} + +Get familiar with constructing strings and printing strings +(i.e.~\texttt{println}, \texttt{mkString}, \texttt{toString}, \ldots) + +\begin{lstlisting}[numbers=none] +scala> List(7,2,3,4,5,6).find(_ < 4) +scala> List(5,6,7,8,9).find(_ < 4) +scala> +\end{lstlisting} + +\subsection*{Task 2 (URLs / Files)} + +ALso Try + +\begin{lstlisting}[numbers=none] +scala> +scala> +scala> +\end{lstlisting} + +\subsection*{Task 3 (Higher-Order Functions)} + +Make sure you understand if-conditions in Scala: They are +\emph{expressions}, meaning they need to calculate a result. For +example an \texttt{if} without an else-branch is nearly always +\emph{not} what you intend to write (because you are not meant to +change any ``state'' outside the expression). Also, remember the quirks +in Scala with if-conditions needing parentheses and there is no +\texttt{then}-keyword. + +\begin{lstlisting}[numbers=none] +scala> val s1 = "foo" +scala> val s2 = "bar" +scala val s3 = "foobar" +scala> if (s1 == s2) print("equal") else print("unequal") +scala> if (s1 ++ s2 == s3) print("equal") else print("unequal") +\end{lstlisting} + +\subsection*{Task 4 (Maps)} + +Write \texttt{for}-comprehensions that enumerate all triples containing the numbers 1 - 5. That is, +print the list + +\[ +\texttt{(1,1,1)}, \texttt{(2,1,1)}, \texttt{(3,1,1)} \;\ldots\; \texttt{(5,5,5)} +\] + +\noindent +Modify the \texttt{for}-comprehensions such that only triples are +printed where the sum is divisible by 3. Then sort the list according +to the middle element (for this use \texttt{sortBy}). + +\subsection*{Task 5 (Pattern-Matching)} + +\subsection*{Task 6 (Web-Crawler)} + +Write a pretty print function for lists of integers which +``condenses'' elements in the list, meaning if there is a number +several times in a row, then print out \mbox{\texttt{n x e}}, where +\texttt{n} is the number of repetitions and \texttt{e} is the +number. For example + +\begin{lstlisting}[numbers=none] + List(1,1,1,2,3,3) => List(3 x 1, 2, 2 x 3) + List(1,2,3,4,4,4) => List(1, 2, 3, 3 x 4) + List(1,1,1,1,1,1) => List(6 x 1) + List(1,1,1,2,1,1) => List(3 x 1, 2, 2 x 1) +\end{lstlisting} + +You might like to define a separate function that first counts the occurences +for each element and returns a list of (Int, Int)-pairs . + +\end{document} + +%%% Local Variables: +%%% mode: latex +%%% TeX-master: t +%%% End: