web代写 | scala作业 | 大数据 – Resit: Evil Wordle Game (Scala, 6 Marks)

Resit: Evil Wordle Game (Scala, 6 Marks)

web代写 | scala作业 | 大数据 – 这道题目是利用web进行的编程代写任务, 涉及了web/scala等代写方面

scala代写 代写scala 大数据代写 big data代做

You are asked to implement a  scala program for making the popular Wordle
game as difficult as possible. The deadline of your submission is on 4th August
at 16:00. There will be no automated tests for the resit, but there are plenty of
testcases in the template and the task description.

Important

  • Make sure the files you submit can be processed by just calling scala <<filename.scala>>on the command line.^1 Use the template files provided and do not make any changes to arguments of functions or to any types. You are free to implement any auxiliary function you might need.
  • Do not leave any test cases running in your code because this might slow down your program! Comment out test cases before submission, otherwise you might hit a timeout.
  • Do not use any mutable data structures in your submissions! They are not needed. This means you cannot create newArrays orListBuffers, for example.
  • Do not usereturnin your code! It has a different meaning in Scala than in Java. It changes the meaning of your program, and you should never use it.
  • Do not usevar! This declares a mutable variable. Only useval!
  • Do not use any parallel collections! No.partherefore! Our testing and marking infrastructure is not set up for it.
Also note that the running time of each part will be restricted to a maximum of
30 seconds on my laptop.

Disclaimer

It should be understood that the work you submit represents your own effort!
You have not copied from anyone else. An exception is the Scala code I showed
during the lectures or uploaded to KEATS, which you can freely use.

(^1) All major OSes, including Windows, have a command line. So there is no good reason to not download Scala, install it and run it on your own computer. Just do it!

Hints

Useful data functions:Source.fromURL,Source.fromFilefor obtaining a web page and reading a file,.getOrElse(..,..) allows to query a Map, but also gives a default value if the Map is not defined, a Map can be updated by using +,.containsand.filtercan test whether an element is included in a list, and respectively filter out elements in a list,.sortBy(_._2)sorts a list of pairs ac cording to the second elements in the pairsthe sorting is done from smallest to highest,.groupByorders lists according to same elements.

Resit (6 Marks, file wordle.scala)

You probably know the game of Wordle^2 where you are supposed to guess a fiveletter word. The feedback for guesses can help with the next guess (green letters are correct, orange letters are present, but in the wrong place). The idea of the program to be implemented here is to make the Wordle game as evil as possible by finding words that are the most difficult to guess. A word list of fiveletter words is available from

https://nms.kcl.ac.uk/christian.urban/wordle.txt (78 KByte)

In your program you need to download this list and implement some functions that in the end select the most difficult words (given an input from the user). If bandwidth is an issue for you, download the file locally, but in the submitted version useSource.fromURLinstead ofSource.fromFile.

Tasks

(1)Implement the functionget_wordle_listwhich takes an URLstring as
argument and requests the corresponding file. The function should re
turn the word list appropriately broken up into lines. The result should
be a list of strings (the lines in the file). In case the url does not produce a
file, return the empty list.
[0.25 Marks]
(2)Implement a polymorphic functionremoveOne, which removes a single
element from a list (if this element is present in the list). For example
removeOne(List(1,2,3,2,1), 3) => List(1, 2, 2, 1)
removeOne(List(1,2,3,2,1), 2) => List(1, 3, 2, 1)
removeOne(List(1,2,3,2,1), 1) => List(2, 3, 2, 1)
removeOne(List(1,2,3,2,1), 0) => List(1, 2, 3, 2, 1)
make sure you only remove a single occurrence of an element from the
list. This function should work for lists of intergers but also lists of chars.
[0.25 Marks]
(3)Implement a functionscorethat calculates the feedback for a word against
a secret word using to the rules of the Wordle game. The output ofscore
should be a list of 5 elements of typeTiprepresenting three outcomes: a
letter in the correct position, a letter that is present, but not in the correct
position and a letter that is absent. For example given the secret word
chess the score for the word caves is
List(Correct, Absent, Absent, Present, Correct)

(^2) https://en.wikipedia.org/wiki/Wordle

You have to be careful with multiple occurrences of letters. For example
the secret chess with the guess swiss should produce
List(Absent, Absent, Absent, Correct, Correct)
even though the first s in swiss is present in the secret word, the s
are already used up by the two letters that are correct. To implement
this you need to implement first a functionpoolwhich calculates all the
letters in a secret that are not correct in a word. For example
pool("chess", "caves") => List(h, e, s)
pool("chess", "swiss") => List(c, h, e)
Now the helper functionauxcan analyse the arguments secret and word
recursively letterbyletter and decide: if the letters are the same, then
returnCorrectfor the corresponding position. If they are not the same,
but the letter is in the pool, then returnPresentand also remove this letter
from the pool in the next recursive call ofaux. Otherwise returnAbsent
for the corresponding position. The functionscoreis a wrapper for the
functionauxcallingauxwith the appropriate arguments (recall what is
calculated withpool). [1.5 Marks]

(4)Implement a functionevalthat gives an integer value to each of theTips such that

eval (Correct)
def
= 10
eval (Present)
def
= 1
eval (Absent)
def
= 0
The functioniscorethen takes an output ofscoreand sums up all cor
responding values. For example for
iscore("chess", "caves") => 21
iscore("chess", "swiss") => 20
[0.5 Marks]

(5)The functioneviltakes a list of secrets (the list from Task 1) and a word as arguments, and calculates the list of words with the lowest score (remem ber we want to make the Wordle game as difficult as possibletherefore when the user gives us a word, we want to find the secrets that produce the lowest score). For this implement a helper functionlowestthat goes through the secrets onebyone and calculates the score. The argument currentis the score of the currently found secrets. When the function lowestis called for the first time then this will be set to the maximum in teger valueInt.MaxValue. The accumulator will be first empty. If a secret is found with the same score ascurrentthen this word is added to the

accumulator. If the secret has a lower score, then the accumulator will be
discarded and this secret will be the new accumulator. If the secret has a
higher score, then it can be ignored. For exampleevil(the wrapper for
lowest) generates
evil(secrets, "stent").length => 1907
evil(secrets, "hexes").length => 2966
evil(secrets, "horse").length => 1203
evil(secrets, "hoise").length => 971
evil(secrets, "house").length => 1228
wheresecretsis the list generated unfer 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.
[1.5 Marks]

(6)The secrets generated in Task 5 are the ones with the lowest score with respect to the word, or the secrets that are furthest away from the given word. This is already quite evil for a secret wordremember we can choose a secret after a user has given a first word. Now we want to make it even more evil by choosing words that have the most obscure letters. For this we calculate the frequency of how many times certain letters occur in our secrets list (see Task 1). The frequency of the letterc, say, is given by the formula

freq(c)
def
= 1  number of occurrences of cnumber of all letters
That means that letters that occur fewer times in our secrets have a higher
frequency. For example the letter y has the frequency 0.
while the much more often occurring letter e has only 0.
(all calculations should be done with Doubles).
The functionfrequenciesshould calculate the frequencies for all lower
case letters by generating a Map from letters to Doubles (frequencies).
[1 Mark]

(7)In this task we want to use the output ofevil, rank each string in the gen erated set and then filter out the strings that are ranked highest (the ones with the most obscure letters). This list of strings often contains only a single word, but in general there might be more (see below). First imple ment a functionrankthat takes a frequency map (from 6) and a string as arguments and generates a rank by summing all frequencies of the letters in the string. For example

rank(frequencies(secrets), "adobe") => 4.
rank(frequencies(secrets), "gaffe") => 4.
rank(frequencies(secrets), "fuzzy") => 4.

Finally, implement a functionranked_evilthat selects from the output ofevilthe string(s) which are highest ranked in evilness.

ranked_evil(secrets, "beats") => List(orlon)
ranked_evil(secrets, "vitae") => List(sools, solos)
ranked_evil(secrets, "bento") => List(assai)
ranked_evil(secrets, "belts") => List(anana)

This means if the user types in beats then the most evil word to choose as secret is orlon (according to our calculations). This word has a zero iscoreand the most obscure letters. [1 Mark]