cws/resit.tex
author Christian Urban <christian.urban@kcl.ac.uk>
Fri, 02 Dec 2022 11:05:41 +0000
changeset 450 61eb4f9b8d84
parent 427 6e93040e3378
child 459 d59404a41d5f
permissions -rw-r--r--
updated
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
427
6e93040e3378 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     1
6e93040e3378 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     2
% !TEX program = xelatex
6e93040e3378 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     3
\documentclass{article}
6e93040e3378 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     4
\usepackage{../styles/style}
6e93040e3378 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     5
\usepackage{disclaimer}
6e93040e3378 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     6
\usepackage{../styles/langs}
6e93040e3378 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     7
\usepackage{graphicx}
6e93040e3378 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     8
6e93040e3378 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     9
\begin{document}
6e93040e3378 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    10
6e93040e3378 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    11
6e93040e3378 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    12
%% should ask to lower case the words.
6e93040e3378 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    13
6e93040e3378 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    14
\section*{Resit: Evil Wordle Game (Scala, 6 Marks)}
6e93040e3378 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    15
6e93040e3378 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    16
6e93040e3378 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    17
\noindent
6e93040e3378 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    18
You are asked to implement a Scala program for making the popular Wordle game as difficult
6e93040e3378 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    19
as possible. The deadline for your submission is on 4th August at 16:00. There will be no
6e93040e3378 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    20
automated tests for the resit, but there are plenty of testcases in the template and the
6e93040e3378 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    21
task description. \bigskip
6e93040e3378 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    22
6e93040e3378 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    23
\IMPORTANTNONE{}
6e93040e3378 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    24
6e93040e3378 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    25
\noindent
6e93040e3378 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    26
Also note that the running time of each part will be restricted to a
6e93040e3378 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    27
maximum of 30 seconds on my laptop.
6e93040e3378 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    28
6e93040e3378 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    29
\DISCLAIMER{}
6e93040e3378 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    30
6e93040e3378 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    31
\subsection*{Hints}
6e93040e3378 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    32
6e93040e3378 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    33
\noindent
6e93040e3378 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    34
Useful data functions: \texttt{Source.fromURL},
6e93040e3378 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    35
\texttt{Source.fromFile} for obtaining a webpage and reading a file,
6e93040e3378 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    36
\texttt{.getOrElse(..,..)} allows to query a Map, but also gives a
6e93040e3378 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    37
default value if the Map is not defined, a Map can be `updated' by
6e93040e3378 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    38
using \texttt{+}, \texttt{.contains} and \texttt{.filter} can test whether
6e93040e3378 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    39
an element is included in a list, and respectively filter out elements in a list,
6e93040e3378 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    40
\texttt{.sortBy(\_.\_2)} sorts a list of pairs according to the second
6e93040e3378 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    41
elements in the pairs---the sorting is done from smallest to highest,
6e93040e3378 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    42
\texttt{.groupBy} orders lists according to same elements
6e93040e3378 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    43
.
6e93040e3378 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    44
6e93040e3378 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    45
6e93040e3378 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    46
\newpage
6e93040e3378 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    47
6e93040e3378 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    48
6e93040e3378 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    49
\subsection*{Resit (6 Marks, file wordle.scala)}
6e93040e3378 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    50
6e93040e3378 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    51
You probably know the game of Wordle\footnote{\url{https://en.wikipedia.org/wiki/Wordle}}
6e93040e3378 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    52
where you are supposed to guess a five-letter word. The feedback for guesses can help
6e93040e3378 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    53
with the next guess (green letters are correct, orange letters are present, but in the
6e93040e3378 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    54
wrong place). For example:
6e93040e3378 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    55
6e93040e3378 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    56
\begin{center}
6e93040e3378 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    57
\includegraphics[scale=0.2]{../pics/w.jpeg}
6e93040e3378 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    58
\end{center}  
6e93040e3378 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    59
6e93040e3378 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    60
\noindent
6e93040e3378 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    61
The idea of the program to be implemented here is to make the Wordle game as evil as possible
6e93040e3378 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    62
by finding words that are the most difficult to guess. A word list of five-letter words is available
6e93040e3378 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    63
from 
6e93040e3378 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    64
6e93040e3378 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    65
\begin{center}
6e93040e3378 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    66
\begin{tabular}{ll}  
6e93040e3378 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    67
  \url{https://nms.kcl.ac.uk/christian.urban/wordle.txt} & (78 KByte)\\
6e93040e3378 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    68
\end{tabular}
6e93040e3378 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    69
\end{center}
6e93040e3378 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    70
6e93040e3378 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    71
\noindent
6e93040e3378 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    72
In your program you need to download this list and implement some
6e93040e3378 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    73
functions that in the end select the most difficult words (given an
6e93040e3378 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    74
input from the user).  If bandwidth is an issue for you, download the
6e93040e3378 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    75
file locally, but in the submitted version use \texttt{Source.fromURL}
6e93040e3378 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    76
instead of \texttt{Source.fromFile}.
6e93040e3378 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    77
6e93040e3378 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    78
\subsection*{Tasks}
6e93040e3378 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    79
6e93040e3378 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    80
\begin{itemize}
6e93040e3378 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    81
\item[(1)] Implement the function \pcode{get_wordle_list} which takes an
6e93040e3378 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    82
  URL-string as argument and requests the corresponding file. The function should
6e93040e3378 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    83
  return the word list appropriately broken up into lines.
6e93040e3378 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    84
  The result should be a list of strings (the lines in the file). In case
6e93040e3378 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    85
  the url does not produce a file, return the empty list.\\
6e93040e3378 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    86
  \mbox{}\hfill [0.25 Marks]
6e93040e3378 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    87
6e93040e3378 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    88
\item[(2)] Implement a polymorphic function \pcode{removeN}, which removes $n$ occurences of an
6e93040e3378 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    89
  element from a list (if this element is less than $n$ times pressent, then remove all occurences).
6e93040e3378 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    90
  For example
6e93040e3378 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    91
6e93040e3378 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    92
\begin{lstlisting}[numbers=none]
6e93040e3378 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    93
removeN(List(1,2,3,2,1), 3, 2)  => List(1, 2, 2, 1)
6e93040e3378 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    94
removeN(List(1,2,3,2,1), 2, 1)  => List(1, 3, 2, 1)
6e93040e3378 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    95
removeN(List(1,2,3,2,1), 2, 2)  => List(1, 3, 1)
6e93040e3378 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    96
removeN(List(1,2,3,2,1), 1, 1)  => List(2, 3, 2, 1)
6e93040e3378 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    97
removeN(List(1,2,3,2,1), 1, 3)  => List(2, 3, 2)
6e93040e3378 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    98
removeN(List(1,2,3,2,1), 0, 2)  => List(1, 2, 3, 2, 1)
6e93040e3378 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    99
\end{lstlisting}
6e93040e3378 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   100
6e93040e3378 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   101
Make sure you only remove at most $n$ occurrences of the element from the list.
6e93040e3378 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   102
This function should work for lists of intergers but also lists of chars, strings etc.\\
6e93040e3378 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   103
  \mbox{}\hfill [0.25 Marks]
6e93040e3378 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   104
6e93040e3378 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   105
\item[(3)] Implement a function \pcode{score} that calculates the
6e93040e3378 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   106
  feedback for a word against a secret word using the rules of the
6e93040e3378 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   107
  Wordle game. The output of \pcode{score} should be a list of 5
6e93040e3378 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   108
  elements of type \pcode{Tip} representing three outcomes: a letter
6e93040e3378 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   109
  in the correct position, a letter that is present, but not in the
6e93040e3378 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   110
  correct position and a letter that is absent. For example given the
6e93040e3378 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   111
  secret word "chess" the score for the word "caves" is
6e93040e3378 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   112
6e93040e3378 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   113
\begin{lstlisting}[numbers=none]
6e93040e3378 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   114
List(Correct, Absent, Absent, Present, Correct)
6e93040e3378 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   115
\end{lstlisting}
6e93040e3378 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   116
6e93040e3378 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   117
  You have to be careful with multiple occurrences of letters. For example
6e93040e3378 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   118
  the secret "chess" with the guess "swiss" should produce
6e93040e3378 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   119
6e93040e3378 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   120
\begin{lstlisting}[numbers=none]
6e93040e3378 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   121
List(Absent, Absent, Absent, Correct, Correct)
6e93040e3378 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   122
\end{lstlisting}
6e93040e3378 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   123
6e93040e3378 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   124
even though the first 's' in "swiss" is present in the secret word, the 's' are already
6e93040e3378 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   125
`used up' by the two letters that are correct. To implement this you need to
6e93040e3378 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   126
implement first a function \pcode{pool} which calculates all the letters in
6e93040e3378 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   127
a secret that are not correct in a word. For example
6e93040e3378 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   128
6e93040e3378 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   129
\begin{lstlisting}[numbers=none]
6e93040e3378 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   130
  pool("chess", "caves")  => List(h, e, s)
6e93040e3378 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   131
  pool("chess", "swiss")  => List(c, h, e)
6e93040e3378 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   132
\end{lstlisting}
6e93040e3378 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   133
6e93040e3378 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   134
  Now the helper function \pcode{aux} can analyse the arguments secret and word recursively letter-by-letter and
6e93040e3378 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   135
  decide: if the letters are the same, then return \pcode{Correct} for the corresponding position.
6e93040e3378 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   136
  If they are not the same, but the letter is in the pool, then return \pcode{Present} and also remove
6e93040e3378 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   137
  this letter from the pool in the next recursive call of \pcode{aux}. Otherwise return \pcode{Absent} for the
6e93040e3378 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   138
  corresponding position. The function \pcode{score} is a wrapper for the function \pcode{aux}
6e93040e3378 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   139
  calling \pcode{aux} with the appropriate arguments (recall what is calculated with \pcode{pool}).\mbox{}\hfill [1.5 Marks]
6e93040e3378 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   140
6e93040e3378 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   141
\item[(4)] Implement a function \pcode{eval} that gives an integer value to each of the
6e93040e3378 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   142
  \pcode{Tip}s such that
6e93040e3378 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   143
6e93040e3378 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   144
  \begin{center}
6e93040e3378 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   145
  \begin{tabular}{lcl}  
6e93040e3378 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   146
    \textit{eval (Correct)} & $\dn$ & $10$\\
6e93040e3378 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   147
    \textit{eval (Present)} & $\dn$ & $1$\\
6e93040e3378 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   148
    \textit{eval (Absent)} & $\dn$ & $0$
6e93040e3378 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   149
  \end{tabular}                                   
6e93040e3378 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   150
  \end{center}  
6e93040e3378 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   151
6e93040e3378 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   152
  The function \pcode{iscore} then takes an output of \pcode{score} and sums
6e93040e3378 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   153
  up all corresponding values. For example for 
6e93040e3378 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   154
6e93040e3378 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   155
\begin{lstlisting}[numbers=none]
6e93040e3378 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   156
  iscore("chess", "caves")  => 21
6e93040e3378 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   157
  iscore("chess", "swiss")  => 20
6e93040e3378 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   158
\end{lstlisting}
6e93040e3378 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   159
  \mbox{}\hfill [0.5 Marks]
6e93040e3378 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   160
6e93040e3378 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   161
\item[(5)] The function \pcode{evil} takes a list of secrets (the list from Task 1)
6e93040e3378 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   162
  and a word as arguments, and calculates the list of words with the lowest
6e93040e3378 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   163
  score (remember we want to make the Wordle game as difficult as possible---therefore
6e93040e3378 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   164
  when the user gives us a word, we want to find the secrets that produce the lowest
6e93040e3378 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   165
  score). For this implement a helper function \pcode{lowest} that goes through
6e93040e3378 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   166
  the secrets one-by-one and calculates the score. The argument \pcode{current} is
6e93040e3378 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   167
  the score of the ``currently'' found secrets. When the function \pcode{lowest}
6e93040e3378 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   168
  is called for the first time then this will be set to the maximum integer value
6e93040e3378 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   169
  \pcode{Int.MaxValue}. The accumulator will be first empty. If a secret is found
6e93040e3378 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   170
  with the same score as \pcode{current} then this word is added to the accumulator.
6e93040e3378 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   171
  If the secret has a lower score, then the accumulator will be discarded and this
6e93040e3378 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   172
  secret will be the new accumulator. If the secret has a higher score, then it can be
6e93040e3378 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   173
  ignored. For example \pcode{evil} (the wrapper for \pcode{lowest}) generates
6e93040e3378 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   174
6e93040e3378 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   175
\begin{lstlisting}[numbers=none]
6e93040e3378 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   176
evil(secrets, "stent").length => 1907
6e93040e3378 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   177
evil(secrets, "hexes").length => 2966
6e93040e3378 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   178
evil(secrets, "horse").length => 1203
6e93040e3378 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   179
evil(secrets, "hoise").length => 971
6e93040e3378 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   180
evil(secrets, "house").length => 1228
6e93040e3378 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   181
\end{lstlisting}
6e93040e3378 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   182
6e93040e3378 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   183
where \pcode{secrets} is the list generated under Task 1.
6e93040e3378 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   184
In all cases above the iscore of the resulting secrets is 0, but this does not need to be the case
6e93040e3378 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   185
in general.\\
6e93040e3378 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   186
  \mbox{}\hfill [1.5 Marks]
6e93040e3378 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   187
6e93040e3378 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   188
\item[(6)] The secrets generated in Task 5 are the ones with the lowest score
6e93040e3378 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   189
  with respect to the word, or the secrets that are furthest ``away'' from the
6e93040e3378 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   190
  given word. This is already quite evil for a secret word---remember we can choose
6e93040e3378 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   191
  a secret \emph{after} a user has given a first word. Now we want to make it
6e93040e3378 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   192
  even more evil by choosing words that have the most obscure letters. For this we
6e93040e3378 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   193
  calculate the frequency of how many times certain letters occur in our secrets
6e93040e3378 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   194
  list (see Task 1). The \emph{frequency} of the letter $c$, say, is given by the formula
6e93040e3378 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   195
6e93040e3378 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   196
  \begin{center}
6e93040e3378 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   197
  $\textit{freq(c)} \dn 1 - \frac{\textit{number of occurrences of c}}{\textit{number of all letters}}$  
6e93040e3378 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   198
  \end{center}  
6e93040e3378 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   199
6e93040e3378 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   200
  That means that letters that occur fewer times in our secrets have a higher frequency.
6e93040e3378 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   201
  For example the letter 'y' has the frequency 0.9680234350909651 while the much more
6e93040e3378 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   202
  often occurring letter 'e' has only 0.897286463151403 (all calculations should be done
6e93040e3378 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   203
  with Doubles).
6e93040e3378 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   204
6e93040e3378 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   205
  The function \pcode{frequencies} should calculate the frequencies for all lower-case letters
6e93040e3378 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   206
  by generating a Map from letters (\pcode{Char}) to Doubles (frequencies).\\ 
6e93040e3378 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   207
  \mbox{}\hfill [1 Mark]
6e93040e3378 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   208
6e93040e3378 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   209
\item[(7)] In this task we want to use the output of \pcode{evil}, rank each string in the
6e93040e3378 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   210
  generated set and then filter out the strings that are ranked highest (the ones with the most obscure letters).
6e93040e3378 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   211
  This list of strings often contains only a single word, but in general there might be more (see below).
6e93040e3378 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   212
  First implement a function \pcode{rank} that takes a frequency map (from 6) and a string as arguments and
6e93040e3378 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   213
  generates a rank by summing up all frequencies of the letters in the string. For example
6e93040e3378 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   214
6e93040e3378 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   215
\begin{lstlisting}[numbers=none]
6e93040e3378 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   216
rank(frequencies(secrets), "adobe") => 4.673604687018193
6e93040e3378 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   217
rank(frequencies(secrets), "gaffe") => 4.745205057045945
6e93040e3378 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   218
rank(frequencies(secrets), "fuzzy") => 4.898735738513722
6e93040e3378 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   219
\end{lstlisting}
6e93040e3378 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   220
6e93040e3378 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   221
  Finally, implement a function \pcode{ranked_evil} that selects from the output of \pcode{evil}
6e93040e3378 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   222
  the string(s) which are highest ranked in evilness.
6e93040e3378 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   223
6e93040e3378 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   224
  
6e93040e3378 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   225
\begin{lstlisting}[numbers=none]
6e93040e3378 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   226
ranked_evil(secrets, "abbey") => List(whizz)
6e93040e3378 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   227
ranked_evil(secrets, "afear") => List(buzzy)
6e93040e3378 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   228
ranked_evil(secrets, "zincy") => List(jugum)
6e93040e3378 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   229
ranked_evil(secrets, "zippy") => List(chuff)
6e93040e3378 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   230
\end{lstlisting}
6e93040e3378 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   231
6e93040e3378 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   232
This means if the user types in "abbey" then the most evil word to choose as secret is "whizzy" (according to
6e93040e3378 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   233
our calculations). This word has a zero \pcode{iscore} and the most obscure letters.
6e93040e3378 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   234
6e93040e3378 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   235
%
6e93040e3378 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   236
%\color{red}
6e93040e3378 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   237
%\section*{Correction with \texttt{ranked\_evil}}
6e93040e3378 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   238
%
6e93040e3378 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   239
%The testcases above are actually not the maximum, but the minimum! I will make sure
6e93040e3378 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   240
%that the task will count as solved when either the minimum (as above) or the maximum (as intended)
6e93040e3378 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   241
%is used. The correct solutions for the above testcases using the maximum are:
6e93040e3378 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   242
%\color{black}
6e93040e3378 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   243
%
6e93040e3378 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   244
%\begin{lstlisting}[numbers=none]
6e93040e3378 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   245
%ranked_evil(secrets, "beats") => List(fuzzy)
6e93040e3378 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   246
%ranked_evil(secrets, "vitae") => List(fuzzy)
6e93040e3378 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   247
%ranked_evil(secrets, "bento") => List(fuzzy)
6e93040e3378 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   248
%ranked_evil(secrets, "belts") => List(fuzzy)
6e93040e3378 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   249
%\end{lstlisting}
6e93040e3378 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   250
%
6e93040e3378 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   251
%\noindent \textcolor{red}{Some further testcases for the maximum are:}
6e93040e3378 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   252
%
6e93040e3378 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   253
%\begin{lstlisting}[numbers=none]
6e93040e3378 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   254
%ranked_evil(secrets, "abbey") => List(whizz)
6e93040e3378 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   255
%ranked_evil(secrets, "afear") => List(buzzy)
6e93040e3378 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   256
%ranked_evil(secrets, "zincy") => List(jugum)
6e93040e3378 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   257
%ranked_evil(secrets, "zippy") => List(chuff)
6e93040e3378 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   258
%\end{lstlisting}
6e93040e3378 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   259
% 
6e93040e3378 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   260
%
6e93040e3378 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   261
6e93040e3378 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   262
\mbox{}\hfill [1 Mark]  
6e93040e3378 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   263
\end{itemize}
6e93040e3378 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   264
6e93040e3378 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   265
\end{document} 
6e93040e3378 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   266
6e93040e3378 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   267
%%% Local Variables: 
6e93040e3378 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   268
%%% mode: latex
6e93040e3378 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   269
%%% TeX-master: t
6e93040e3378 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   270
%%% End: 
6e93040e3378 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   271
6e93040e3378 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   272
6e93040e3378 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   273