cws/resit.tex
author Christian Urban <christian.urban@kcl.ac.uk>
Sat, 14 Jan 2023 14:13:16 +0000
changeset 459 d59404a41d5f
parent 427 6e93040e3378
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
459
d59404a41d5f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 427
diff changeset
    14
\section*{Resit: Evil Wordle Game (Scala, 7 Marks)}
427
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
459
d59404a41d5f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 427
diff changeset
    19
as possible. The deadline for your submission is on 24th January at 23:00. There will be no
427
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
459
d59404a41d5f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 427
diff changeset
    49
\subsection*{Resit (7 Marks, file wordle.scala)}
427
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
459
d59404a41d5f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 427
diff changeset
    78
427
6e93040e3378 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    79
\subsection*{Tasks}
6e93040e3378 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    80
6e93040e3378 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    81
\begin{itemize}
6e93040e3378 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    82
\item[(1)] Implement the function \pcode{get_wordle_list} which takes an
6e93040e3378 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    83
  URL-string as argument and requests the corresponding file. The function should
6e93040e3378 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    84
  return the word list appropriately broken up into lines.
6e93040e3378 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    85
  The result should be a list of strings (the lines in the file). In case
459
d59404a41d5f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 427
diff changeset
    86
  the url does not produce a file, return the empty list.
427
6e93040e3378 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    87
459
d59404a41d5f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 427
diff changeset
    88
  In what follows we will use \texttt{secrets} to refer  to the list of words listed
d59404a41d5f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 427
diff changeset
    89
  in \texttt{wordle.txt}.  
d59404a41d5f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 427
diff changeset
    90
  
d59404a41d5f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 427
diff changeset
    91
  \mbox{}\hfill [0.5 Marks]
d59404a41d5f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 427
diff changeset
    92
d59404a41d5f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 427
diff changeset
    93
\item[(2)] Implement a polymorphic function \pcode{removeN}, which removes $n$ occurrences of an
d59404a41d5f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 427
diff changeset
    94
  element from a list (if this element is less than $n$ times present, then remove all occurrences).
427
6e93040e3378 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    95
  For example
6e93040e3378 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    96
6e93040e3378 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    97
\begin{lstlisting}[numbers=none]
459
d59404a41d5f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 427
diff changeset
    98
removeN(List(1,2,3,2,1), 3, 2) => List(1, 2, 2, 1)
d59404a41d5f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 427
diff changeset
    99
removeN(List(1,2,3,2,1), 2, 1) => List(1, 3, 2, 1)
d59404a41d5f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 427
diff changeset
   100
removeN(List(1,2,3,2,1), 2, 2) => List(1, 3, 1)
d59404a41d5f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 427
diff changeset
   101
removeN(List(1,2,3,2,1), 1, 1) => List(2, 3, 2, 1)
d59404a41d5f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 427
diff changeset
   102
removeN(List(1,2,3,2,1), 1, 3) => List(2, 3, 2)
d59404a41d5f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 427
diff changeset
   103
removeN(List(1,2,3,2,1), 0, 2) => List(1, 2, 3, 2, 1)
d59404a41d5f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 427
diff changeset
   104
removeN(List("1","2","3","2","1"), "1", 1)
d59404a41d5f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 427
diff changeset
   105
                           => List("2","3","2","1")
427
6e93040e3378 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   106
\end{lstlisting}
6e93040e3378 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   107
6e93040e3378 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   108
Make sure you only remove at most $n$ occurrences of the element from the list.
459
d59404a41d5f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 427
diff changeset
   109
This function should work for lists of integers but also lists of chars, strings etc.\\
d59404a41d5f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 427
diff changeset
   110
  \mbox{}\hfill [0.5 Marks]
427
6e93040e3378 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   111
6e93040e3378 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   112
\item[(3)] Implement a function \pcode{score} that calculates the
6e93040e3378 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   113
  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
   114
  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
   115
  elements of type \pcode{Tip} representing three outcomes: a letter
6e93040e3378 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   116
  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
   117
  correct position and a letter that is absent. For example given the
6e93040e3378 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   118
  secret word "chess" the score for the word "caves" is
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(Correct, Absent, Absent, Present, 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
  You have to be careful with multiple occurrences of letters. For example
6e93040e3378 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   125
  the secret "chess" with the guess "swiss" should produce
6e93040e3378 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   126
6e93040e3378 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   127
\begin{lstlisting}[numbers=none]
6e93040e3378 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   128
List(Absent, Absent, Absent, Correct, Correct)
6e93040e3378 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   129
\end{lstlisting}
6e93040e3378 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   130
6e93040e3378 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   131
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
   132
`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
   133
implement first a function \pcode{pool} which calculates all the letters in
6e93040e3378 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   134
a secret that are not correct in a word. For example
6e93040e3378 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   135
6e93040e3378 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   136
\begin{lstlisting}[numbers=none]
6e93040e3378 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   137
  pool("chess", "caves")  => List(h, e, s)
6e93040e3378 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   138
  pool("chess", "swiss")  => List(c, h, e)
6e93040e3378 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   139
\end{lstlisting}
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
  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
   142
  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
   143
  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
   144
  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
   145
  corresponding position. The function \pcode{score} is a wrapper for the function \pcode{aux}
459
d59404a41d5f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 427
diff changeset
   146
  calling \pcode{aux} with the appropriate arguments (recall what is calculated with \pcode{pool}).\mbox{}\hfill [2 Marks]
427
6e93040e3378 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   147
6e93040e3378 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   148
\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
   149
  \pcode{Tip}s such that
6e93040e3378 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   150
6e93040e3378 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   151
  \begin{center}
6e93040e3378 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   152
  \begin{tabular}{lcl}  
6e93040e3378 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   153
    \textit{eval (Correct)} & $\dn$ & $10$\\
6e93040e3378 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   154
    \textit{eval (Present)} & $\dn$ & $1$\\
6e93040e3378 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   155
    \textit{eval (Absent)} & $\dn$ & $0$
6e93040e3378 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   156
  \end{tabular}                                   
6e93040e3378 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   157
  \end{center}  
6e93040e3378 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   158
6e93040e3378 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   159
  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
   160
  up all corresponding values. For example for 
6e93040e3378 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   161
6e93040e3378 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   162
\begin{lstlisting}[numbers=none]
6e93040e3378 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   163
  iscore("chess", "caves")  => 21
6e93040e3378 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   164
  iscore("chess", "swiss")  => 20
6e93040e3378 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   165
\end{lstlisting}
6e93040e3378 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   166
  \mbox{}\hfill [0.5 Marks]
6e93040e3378 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   167
6e93040e3378 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   168
\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
   169
  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
   170
  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
   171
  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
   172
  score). For this implement a helper function \pcode{lowest} that goes through
6e93040e3378 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   173
  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
   174
  the score of the ``currently'' found secrets. When the function \pcode{lowest}
6e93040e3378 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   175
  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
   176
  \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
   177
  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
   178
  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
   179
  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
   180
  ignored. For example \pcode{evil} (the wrapper for \pcode{lowest}) generates
6e93040e3378 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   181
6e93040e3378 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   182
\begin{lstlisting}[numbers=none]
6e93040e3378 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   183
evil(secrets, "stent").length => 1907
6e93040e3378 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   184
evil(secrets, "hexes").length => 2966
6e93040e3378 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   185
evil(secrets, "horse").length => 1203
6e93040e3378 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   186
evil(secrets, "hoise").length => 971
6e93040e3378 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   187
evil(secrets, "house").length => 1228
6e93040e3378 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   188
\end{lstlisting}
6e93040e3378 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   189
6e93040e3378 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   190
where \pcode{secrets} is the list generated under Task 1.
6e93040e3378 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   191
In all cases above the iscore of the resulting secrets is 0, but this does not need to be the case
459
d59404a41d5f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 427
diff changeset
   192
in general.
d59404a41d5f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 427
diff changeset
   193
427
6e93040e3378 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   194
  \mbox{}\hfill [1.5 Marks]
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
\item[(6)] The secrets generated in Task 5 are the ones with the lowest score
459
d59404a41d5f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 427
diff changeset
   197
  with respect to the word. You can think of these as the secrets that are furthest ``away'' from the
427
6e93040e3378 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   198
  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
   199
  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
   200
  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
   201
  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
   202
  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
   203
6e93040e3378 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   204
  \begin{center}
6e93040e3378 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   205
  $\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
   206
  \end{center}  
6e93040e3378 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   207
6e93040e3378 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   208
  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
   209
  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
   210
  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
   211
  with Doubles).
6e93040e3378 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   212
6e93040e3378 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   213
  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
   214
  by generating a Map from letters (\pcode{Char}) to Doubles (frequencies).\\ 
6e93040e3378 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   215
  \mbox{}\hfill [1 Mark]
6e93040e3378 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   216
6e93040e3378 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   217
\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
   218
  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
   219
  This list of strings often contains only a single word, but in general there might be more (see below).
459
d59404a41d5f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 427
diff changeset
   220
  First implement a function \pcode{rank} that takes a frequency map (from 6) and a string as arguments.
d59404a41d5f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 427
diff changeset
   221
  In the testcases, the frequency map is generated for all words in \texttt{secrets}, that is the
d59404a41d5f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 427
diff changeset
   222
  whole list in \texttt{wordle.txt}. The function  
427
6e93040e3378 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   223
  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
   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
rank(frequencies(secrets), "adobe") => 4.673604687018193
6e93040e3378 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   227
rank(frequencies(secrets), "gaffe") => 4.745205057045945
6e93040e3378 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   228
rank(frequencies(secrets), "fuzzy") => 4.898735738513722
6e93040e3378 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   229
\end{lstlisting}
6e93040e3378 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   230
6e93040e3378 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   231
  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
   232
  the string(s) which are highest ranked in evilness.
6e93040e3378 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   233
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
\begin{lstlisting}[numbers=none]
6e93040e3378 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   236
ranked_evil(secrets, "abbey") => List(whizz)
6e93040e3378 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   237
ranked_evil(secrets, "afear") => List(buzzy)
6e93040e3378 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   238
ranked_evil(secrets, "zincy") => List(jugum)
6e93040e3378 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   239
ranked_evil(secrets, "zippy") => List(chuff)
6e93040e3378 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   240
\end{lstlisting}
6e93040e3378 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   241
459
d59404a41d5f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 427
diff changeset
   242
This means if the user types in "abbey" then the most evil word to choose as secret is ``whizz'' (according to
427
6e93040e3378 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   243
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
   244
6e93040e3378 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   245
\mbox{}\hfill [1 Mark]  
6e93040e3378 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   246
\end{itemize}
6e93040e3378 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   247
6e93040e3378 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   248
\end{document} 
6e93040e3378 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   249
6e93040e3378 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   250
%%% Local Variables: 
6e93040e3378 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   251
%%% mode: latex
6e93040e3378 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   252
%%% TeX-master: t
6e93040e3378 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   253
%%% End: 
6e93040e3378 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   254
6e93040e3378 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   255
6e93040e3378 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   256