cws/main_cw02.tex
changeset 475 59e005dcf163
parent 459 d59404a41d5f
equal deleted inserted replaced
474:b528d1d3d3c3 475:59e005dcf163
    38 to GitHub and receive (after sometimes a long delay) some automated
    38 to GitHub and receive (after sometimes a long delay) some automated
    39 feedback. In the end we will take a snapshot of the submitted files
    39 feedback. In the end we will take a snapshot of the submitted files
    40 and apply an automated marking script to them.\medskip
    40 and apply an automated marking script to them.\medskip
    41 
    41 
    42 \noindent
    42 \noindent
    43 In addition, the Scala part comes with reference
    43 In addition, the Scala part comes with a reference
    44 implementations in form of \texttt{jar}-files. This allows you to run
    44 implementation in form of \texttt{jar}-files. This allows you to run
    45 any test cases on your own computer. For example you can call Scala on
    45 any test cases on your own computer. For example you can call \texttt{scala-cli} on
    46 the command line with the option \texttt{-cp wordle.jar} and then
    46 the command line with the option \texttt{--extra-jars wordle.jar} and then
    47 query any function from the template file. Say you want to find out
    47 query any function from the template file. Say you want to find out
    48 what the function \texttt{} produces: for this you just need
    48 what the function \texttt{} produces: for this you just need
    49 to prefix it with the object name \texttt{M2}.  If you want to find out what
    49 to prefix it with the object name \texttt{M2}.  If you want to find out what
    50 these functions produce for the list \texttt{List("a", "b", "b")},
    50 these functions produce for the list \texttt{List("a", "b", "b")},
    51 you would type something like:
    51 you would type something like:
    52 
    52 
    53 \begin{lstlisting}[language={},numbers=none,basicstyle=\ttfamily\small]
    53 \begin{lstlisting}[language={},numbers=none,basicstyle=\ttfamily\small]
    54 $ scala -cp wordle.jar
    54 $ scala-cli --extra-jars wordle.jar
    55 scala> val secretsURL =
    55 scala> val secretsURL =
    56      | """https://nms.kcl.ac.uk/christian.urban/wordle.txt"""
    56      | """https://nms.kcl.ac.uk/christian.urban/wordle.txt"""
    57 
    57 
    58 scala> M2.get_wordle_list(secretsURL)
    58 scala> M2.get_wordle_list(secretsURL)
    59 val res0: List[String] = List(aahed, aalii, ...)
    59 val res0: List[String] = List(aahed, aalii, ...)
   113   URL-string as argument and requests the corresponding file. The function should
   113   URL-string as argument and requests the corresponding file. The function should
   114   return the word list appropriately broken up into lines.
   114   return the word list appropriately broken up into lines.
   115   The result should be a list of strings (the lines in the file). In case
   115   The result should be a list of strings (the lines in the file). In case
   116   the url does not produce a file, return the empty list.
   116   the url does not produce a file, return the empty list.
   117 
   117 
   118   \textcolor{red}{
   118   In what follows we will use \texttt{secrets} to refer  to the list of words listed
   119     In what follows we will use \texttt{secrets} to refer  to the list of words listed
   119   in \texttt{wordle.txt}.\\  
   120     in \texttt{wordle.txt}.  
       
   121   }
       
   122   \mbox{}\hfill [0.5 Marks]
   120   \mbox{}\hfill [0.5 Marks]
   123 
   121 
   124 \item[(2)] Implement a polymorphic function \pcode{removeN}, which removes $n$ occurrences of an
   122 \item[(2)] Implement a polymorphic function \pcode{removeN}, which removes $n$ occurrences of an
   125   element from a list (if this element is less than $n$ times present, then remove all occurrences).
   123   element from a list (if this element is less than $n$ times present, then remove all occurrences).
   126   For example
   124   For example
   216 evil(secrets, "house").length => 1228
   214 evil(secrets, "house").length => 1228
   217 \end{lstlisting}
   215 \end{lstlisting}
   218 
   216 
   219 where \pcode{secrets} is the list generated under Task 1.
   217 where \pcode{secrets} is the list generated under Task 1.
   220 In all cases above the iscore of the resulting secrets is 0, but this does not need to be the case
   218 In all cases above the iscore of the resulting secrets is 0, but this does not need to be the case
   221 in general.
   219 in general.\\
   222 
   220 \mbox{}\hfill [1.5 Marks]
   223 \color{red}
       
   224   Note that the template gives as type for \texttt{evil}:
       
   225 
       
   226   \begin{center}
       
   227   \texttt{def evil(secrets: List[String], word: String) = ???}  
       
   228   \end{center}
       
   229 
       
   230   where the return type is left unspecified. This return type is not needed when
       
   231   functions are not recursive---\texttt{evil} is meant to be just a wrapper that
       
   232   calls \texttt{lowest} with appropriate default arguments and returns whatever
       
   233   \texttt{lowest} returns. Therefore a return type is not needed. But a slightly
       
   234   more accurate template definition for \texttt{evil} is:\medskip\medskip
       
   235 
       
   236   \begin{minipage}{1.05\textwidth}
       
   237   \begin{center}
       
   238   \texttt{def evil(secrets: List[String], word: String) : List[String] = ???}  
       
   239   \end{center}
       
   240   \end{minipage}\medskip\medskip
       
   241 
       
   242   where also the return type is explicitly given.\\\color{black}
       
   243   \mbox{}\hfill [1.5 Marks]
       
   244 
   221 
   245 \item[(6)] The secrets generated in Task 5 are the ones with the lowest score
   222 \item[(6)] The secrets generated in Task 5 are the ones with the lowest score
   246   with respect to the word. You can think of these as the secrets that are furthest ``away'' from the
   223   with respect to the word. You can think of these as the secrets that are furthest ``away'' from the
   247   given word. This is already quite evil for a secret word---remember we can choose
   224   given word. This is already quite evil for a secret word---remember we can choose
   248   a secret \emph{after} a user has given a first word. Now we want to make it
   225   a secret \emph{after} a user has given a first word. Now we want to make it
   265 
   242 
   266 \item[(7)] In this task we want to use the output of \pcode{evil}, rank each string in the
   243 \item[(7)] In this task we want to use the output of \pcode{evil}, rank each string in the
   267   generated set and then filter out the strings that are ranked highest (the ones with the most obscure letters).
   244   generated set and then filter out the strings that are ranked highest (the ones with the most obscure letters).
   268   This list of strings often contains only a single word, but in general there might be more (see below).
   245   This list of strings often contains only a single word, but in general there might be more (see below).
   269   First implement a function \pcode{rank} that takes a frequency map (from 6) and a string as arguments.
   246   First implement a function \pcode{rank} that takes a frequency map (from 6) and a string as arguments.
   270   \textcolor{red}{In the testcases, the frequency map is generated for all words in \texttt{secrets}, that is the
   247   In the testcases, the frequency map is generated for all words in \texttt{secrets}, that is the
   271   whole list in \texttt{wordle.txt}.} The function  
   248   whole list in \texttt{wordle.txt}. The function  
   272   generates a rank by summing up all frequencies of the letters in the string. For example
   249   generates a rank by summing up all frequencies of the letters in the string. For example
   273 
   250 
   274 \begin{lstlisting}[numbers=none]
   251 \begin{lstlisting}[numbers=none]
   275 rank(frequencies(secrets), "adobe") => 4.673604687018193
   252 rank(frequencies(secrets), "adobe") => 4.673604687018193
   276 rank(frequencies(secrets), "gaffe") => 4.745205057045945
   253 rank(frequencies(secrets), "gaffe") => 4.745205057045945
   277 rank(frequencies(secrets), "fuzzy") => 4.898735738513722
   254 rank(frequencies(secrets), "fuzzy") => 4.898735738513722
   278 \end{lstlisting}
   255 \end{lstlisting}
   279 
   256 
   280   \color{red}
       
   281   The return type for \texttt{rank} is \texttt{Double}:
       
   282 
       
   283   \begin{center}
       
   284   \texttt{def rank(frqs: Map[Char, Double], s: String) : Double = ???}  
       
   285   \end{center}
       
   286   \color{black}
       
   287 
   257 
   288   Finally, implement a function \pcode{ranked_evil} that selects from the output of \pcode{evil}
   258   Finally, implement a function \pcode{ranked_evil} that selects from the output of \pcode{evil}
   289   the string(s) which are highest ranked in evilness.
   259   the string(s) which are highest ranked in evilness.
   290 
   260 
   291   
   261   
   296 ranked_evil(secrets, "zippy") => List(chuff)
   266 ranked_evil(secrets, "zippy") => List(chuff)
   297 \end{lstlisting}
   267 \end{lstlisting}
   298 
   268 
   299 This means if the user types in "abbey" then the most evil word to choose as secret is ``whizz'' (according to
   269 This means if the user types in "abbey" then the most evil word to choose as secret is ``whizz'' (according to
   300 our calculations). This word has a zero \pcode{iscore} and the most obscure letters.
   270 our calculations). This word has a zero \pcode{iscore} and the most obscure letters.
   301 
       
   302 \color{red}
       
   303   The return type for \texttt{ranked\_evil} is \texttt{List[String]}:
       
   304 
       
   305   \begin{center}
       
   306   \texttt{def ranked\_evil(secrets: List[String], word: String) : List[String] = ???}  
       
   307   \end{center}
       
   308   \color{black}
       
   309 
   271 
   310 %
   272 %
   311 %\color{red}
   273 %\color{red}
   312 %\section*{Correction with \texttt{ranked\_evil}}
   274 %\section*{Correction with \texttt{ranked\_evil}}
   313 %
   275 %