author | Christian Urban <christian.urban@kcl.ac.uk> |
Fri, 23 Dec 2022 16:52:34 +0000 | |
changeset 456 | d076cb2e0b75 |
parent 439 | 97594b9998a8 |
child 463 | 0315d9983cd0 |
permissions | -rw-r--r-- |
439 | 1 |
// Main Part 3 about Evil Wordle |
2 |
//=============================== |
|
435
fda7c39f3b6a
updated wordle testing
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
3 |
|
439 | 4 |
// test bash |
5 |
||
6 |
object M2 { |
|
435
fda7c39f3b6a
updated wordle testing
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
7 |
|
fda7c39f3b6a
updated wordle testing
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
8 |
import io.Source |
fda7c39f3b6a
updated wordle testing
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
9 |
import scala.util._ |
fda7c39f3b6a
updated wordle testing
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
10 |
|
439 | 11 |
// ADD YOUR CODE BELOW |
12 |
//====================== |
|
435
fda7c39f3b6a
updated wordle testing
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
13 |
|
439 | 14 |
// test import |
15 |
||
16 |
||
17 |
//(1) |
|
435
fda7c39f3b6a
updated wordle testing
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
18 |
def get_wordle_list(url: String) : List[String] = { |
439 | 19 |
Try(Source.fromURL(url)("ISO-8859-1").getLines().toList).getOrElse(Nil) |
435
fda7c39f3b6a
updated wordle testing
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
20 |
} |
fda7c39f3b6a
updated wordle testing
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
21 |
|
fda7c39f3b6a
updated wordle testing
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
22 |
|
fda7c39f3b6a
updated wordle testing
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
23 |
// val secrets = get_wordle_list("https://nms.kcl.ac.uk/christian.urban/wordle.txt") |
439 | 24 |
// secrets.length // => 12972 |
25 |
// secrets.filter(_.length != 5) // => Nil |
|
435
fda7c39f3b6a
updated wordle testing
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
26 |
|
439 | 27 |
//(2) |
28 |
def removeN[A](xs: List[A], elem: A, n: Int) : List[A] = { |
|
29 |
if (n == 0) xs |
|
30 |
else xs match { |
|
31 |
case Nil => Nil |
|
32 |
case x::xs => |
|
33 |
if (x == elem) removeN(xs, x, n - 1) |
|
34 |
else x::removeN(xs, elem, n) |
|
35 |
} |
|
36 |
} |
|
435
fda7c39f3b6a
updated wordle testing
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
37 |
|
fda7c39f3b6a
updated wordle testing
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
38 |
|
439 | 39 |
// removeN(List(1,2,3,2,1), 3, 1) // => List(1, 2, 2, 1) |
40 |
// removeN(List(1,2,3,2,1), 2, 1) // => List(1, 3, 2, 1) |
|
41 |
// removeN(List(1,2,3,2,1), 1, 1) // => List(2, 3, 2, 1) |
|
42 |
// removeN(List(1,2,3,2,1), 0, 2) // => List(1, 2, 3, 2, 1) |
|
435
fda7c39f3b6a
updated wordle testing
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
43 |
|
439 | 44 |
// (3) |
435
fda7c39f3b6a
updated wordle testing
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
45 |
abstract class Tip |
fda7c39f3b6a
updated wordle testing
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
46 |
case object Absent extends Tip |
fda7c39f3b6a
updated wordle testing
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
47 |
case object Present extends Tip |
fda7c39f3b6a
updated wordle testing
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
48 |
case object Correct extends Tip |
fda7c39f3b6a
updated wordle testing
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
49 |
|
439 | 50 |
|
51 |
def pool(secret: String, word: String) : List[Char] = { |
|
435
fda7c39f3b6a
updated wordle testing
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
52 |
for (i <- (0 to 4).toList |
fda7c39f3b6a
updated wordle testing
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
53 |
if secret(i) != word(i)) |
fda7c39f3b6a
updated wordle testing
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
54 |
yield secret(i) |
fda7c39f3b6a
updated wordle testing
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
55 |
} |
fda7c39f3b6a
updated wordle testing
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
56 |
|
fda7c39f3b6a
updated wordle testing
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
57 |
def aux(secret: List[Char], word: List[Char], pool: List[Char]) : List[Tip] = (secret, word) match { |
fda7c39f3b6a
updated wordle testing
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
58 |
case (Nil, Nil) => Nil |
fda7c39f3b6a
updated wordle testing
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
59 |
case (s::srest, w::wrest) => { |
fda7c39f3b6a
updated wordle testing
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
60 |
if (s == w) Correct::aux(srest, wrest, pool) |
439 | 61 |
else if (pool.contains(w)) Present::aux(srest, wrest, removeN(pool, w, 1)) |
435
fda7c39f3b6a
updated wordle testing
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
62 |
else Absent::aux(srest, wrest, pool) |
fda7c39f3b6a
updated wordle testing
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
63 |
} |
fda7c39f3b6a
updated wordle testing
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
64 |
} |
fda7c39f3b6a
updated wordle testing
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
65 |
|
fda7c39f3b6a
updated wordle testing
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
66 |
|
439 | 67 |
def score(secret: String, word: String) : List[Tip] = |
68 |
aux(secret.toList, word.toList, pool(secret, word)) |
|
435
fda7c39f3b6a
updated wordle testing
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
69 |
|
fda7c39f3b6a
updated wordle testing
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
70 |
|
439 | 71 |
// score("chess", "caves") // => List(Correct, Absent, Absent, Present, Correct) |
72 |
// score("doses", "slide") // => List(Present, Absent, Absent, Present, Present) |
|
73 |
// score("chess", "swiss") // => List(Absent, Absent, Absent, Correct, Correct) |
|
74 |
// score("chess", "eexss") // => List(Present, Absent, Absent, Correct, Correct) |
|
435
fda7c39f3b6a
updated wordle testing
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
75 |
|
439 | 76 |
// (4) |
435
fda7c39f3b6a
updated wordle testing
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
77 |
def eval(t: Tip) : Int = t match { |
fda7c39f3b6a
updated wordle testing
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
78 |
case Correct => 10 |
fda7c39f3b6a
updated wordle testing
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
79 |
case Present => 1 |
fda7c39f3b6a
updated wordle testing
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
80 |
case Absent => 0 |
fda7c39f3b6a
updated wordle testing
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
81 |
} |
fda7c39f3b6a
updated wordle testing
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
82 |
|
439 | 83 |
def iscore(secret: String, word: String) : Int = |
84 |
score(secret, word).map(eval).sum |
|
85 |
||
86 |
//iscore("chess", "caves") // => 21 |
|
87 |
//iscore("chess", "swiss") // => 20 |
|
88 |
||
89 |
// (5) |
|
90 |
def lowest(secrets: List[String], word: String, current: Int, acc: List[String]) : List[String] = secrets match { |
|
91 |
case Nil => acc |
|
92 |
case s::srest => { |
|
93 |
val nscore = iscore(s, word) |
|
94 |
if (nscore < current) lowest(srest, word, nscore, List(s)) |
|
95 |
else if (nscore == current) lowest(srest, word, current, s::acc) |
|
96 |
else lowest(srest, word, current, acc) |
|
97 |
} |
|
435
fda7c39f3b6a
updated wordle testing
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
98 |
} |
fda7c39f3b6a
updated wordle testing
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
99 |
|
439 | 100 |
def evil(secrets: List[String], word: String) = |
101 |
lowest(secrets, word, Int.MaxValue, Nil) |
|
435
fda7c39f3b6a
updated wordle testing
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
102 |
|
fda7c39f3b6a
updated wordle testing
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
103 |
|
439 | 104 |
//evil(secrets, "stent").length |
105 |
//evil(secrets, "hexes").length |
|
106 |
//evil(secrets, "horse").length |
|
107 |
//evil(secrets, "hoise").length |
|
108 |
//evil(secrets, "house").length |
|
435
fda7c39f3b6a
updated wordle testing
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
109 |
|
439 | 110 |
// (6) |
435
fda7c39f3b6a
updated wordle testing
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
111 |
def frequencies(secrets: List[String]) : Map[Char, Double] = { |
fda7c39f3b6a
updated wordle testing
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
112 |
val all = secrets.flatten |
fda7c39f3b6a
updated wordle testing
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
113 |
all.groupBy(identity).view.mapValues(1.0D - _.size.toDouble / all.size ).toMap |
fda7c39f3b6a
updated wordle testing
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
114 |
} |
fda7c39f3b6a
updated wordle testing
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
115 |
|
439 | 116 |
// (7) |
435
fda7c39f3b6a
updated wordle testing
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
117 |
def rank(frqs: Map[Char, Double], s: String) = { |
fda7c39f3b6a
updated wordle testing
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
118 |
s.map(frqs(_)).sum |
fda7c39f3b6a
updated wordle testing
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
119 |
} |
fda7c39f3b6a
updated wordle testing
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
120 |
|
439 | 121 |
|
435
fda7c39f3b6a
updated wordle testing
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
122 |
def ranked_evil(secrets: List[String], word: String) = { |
fda7c39f3b6a
updated wordle testing
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
123 |
val frqs = frequencies(secrets) |
fda7c39f3b6a
updated wordle testing
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
124 |
val ev = evil(secrets, word) |
fda7c39f3b6a
updated wordle testing
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
125 |
ev.groupBy(rank(frqs, _)).toList.sortBy(_._1).reverse.head._2 |
fda7c39f3b6a
updated wordle testing
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
126 |
} |
fda7c39f3b6a
updated wordle testing
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
127 |
|
439 | 128 |
|
435
fda7c39f3b6a
updated wordle testing
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
129 |
} |
fda7c39f3b6a
updated wordle testing
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
130 |
|
439 | 131 |
|
132 |
||
133 |
||
134 |
||
435
fda7c39f3b6a
updated wordle testing
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
135 |
|
439 | 136 |
// This template code is subject to copyright |
137 |
// by King's College London, 2022. Do not |
|
138 |
// make the template code public in any shape |
|
139 |
// or form, and do not exchange it with other |
|
140 |
// students under any circumstance. |