author | Christian Urban <christian.urban@kcl.ac.uk> |
Thu, 06 Jun 2024 22:18:15 +0100 | |
changeset 490 | 4778fefecd0c |
parent 475 | 59e005dcf163 |
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 |
|
5 |
object M2 { |
|
435
fda7c39f3b6a
updated wordle testing
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
6 |
|
fda7c39f3b6a
updated wordle testing
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
7 |
import io.Source |
fda7c39f3b6a
updated wordle testing
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
8 |
import scala.util._ |
fda7c39f3b6a
updated wordle testing
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
9 |
|
439 | 10 |
// ADD YOUR CODE BELOW |
11 |
//====================== |
|
435
fda7c39f3b6a
updated wordle testing
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
12 |
|
439 | 13 |
|
14 |
//(1) |
|
435
fda7c39f3b6a
updated wordle testing
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
15 |
def get_wordle_list(url: String) : List[String] = { |
475 | 16 |
Try(Source.fromURL(url).getLines.toList).getOrElse(List()) |
435
fda7c39f3b6a
updated wordle testing
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
17 |
} |
475 | 18 |
|
435
fda7c39f3b6a
updated wordle testing
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
19 |
// val secrets = get_wordle_list("https://nms.kcl.ac.uk/christian.urban/wordle.txt") |
439 | 20 |
// secrets.length // => 12972 |
21 |
// secrets.filter(_.length != 5) // => Nil |
|
435
fda7c39f3b6a
updated wordle testing
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
22 |
|
475 | 23 |
//(2) |
24 |
def removeN[A](xs: List[A], elem: A, n: Int) : List[A] = xs match { |
|
25 |
case Nil => Nil |
|
26 |
case x :: tail if x == elem && n > 0 => removeN(tail, elem, n - 1) |
|
27 |
case x :: tail => x :: removeN(tail, elem, n) |
|
463 | 28 |
} |
435
fda7c39f3b6a
updated wordle testing
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
29 |
|
475 | 30 |
|
439 | 31 |
// removeN(List(1,2,3,2,1), 3, 1) // => List(1, 2, 2, 1) |
32 |
// removeN(List(1,2,3,2,1), 2, 1) // => List(1, 3, 2, 1) |
|
33 |
// removeN(List(1,2,3,2,1), 1, 1) // => List(2, 3, 2, 1) |
|
34 |
// 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
|
35 |
|
439 | 36 |
// (3) |
435
fda7c39f3b6a
updated wordle testing
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
37 |
abstract class Tip |
fda7c39f3b6a
updated wordle testing
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
38 |
case object Absent extends Tip |
fda7c39f3b6a
updated wordle testing
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
39 |
case object Present extends Tip |
fda7c39f3b6a
updated wordle testing
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
40 |
case object Correct extends Tip |
fda7c39f3b6a
updated wordle testing
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
41 |
|
439 | 42 |
|
43 |
def pool(secret: String, word: String) : List[Char] = { |
|
475 | 44 |
(for(index <- (0 to word.length-1); if (word(index) != secret(index))) yield secret(index).toChar).toList |
435
fda7c39f3b6a
updated wordle testing
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
45 |
} |
475 | 46 |
// t |
463 | 47 |
def aux(secret: List[Char], word: List[Char], pool: List[Char]) : List[Tip] = { |
475 | 48 |
if (secret.isEmpty) List() |
463 | 49 |
else { |
475 | 50 |
if (secret.head == word.head) Correct :: aux(secret.tail, word.tail, pool) |
51 |
else if (pool.contains(word.head)) Present :: aux(secret.tail, word.tail, pool.filterNot(_ == word.head)) |
|
52 |
else Absent :: aux(secret.tail, word.tail, pool) |
|
435
fda7c39f3b6a
updated wordle testing
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
53 |
} |
fda7c39f3b6a
updated wordle testing
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
54 |
} |
fda7c39f3b6a
updated wordle testing
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
55 |
|
475 | 56 |
def score(secret: String, word: String) : List[Tip] = aux(secret.toList, word.toList, pool(secret, word)) |
435
fda7c39f3b6a
updated wordle testing
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
57 |
|
fda7c39f3b6a
updated wordle testing
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
58 |
|
439 | 59 |
// score("chess", "caves") // => List(Correct, Absent, Absent, Present, Correct) |
60 |
// score("doses", "slide") // => List(Present, Absent, Absent, Present, Present) |
|
61 |
// score("chess", "swiss") // => List(Absent, Absent, Absent, Correct, Correct) |
|
62 |
// score("chess", "eexss") // => List(Present, Absent, Absent, Correct, Correct) |
|
435
fda7c39f3b6a
updated wordle testing
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
63 |
|
439 | 64 |
// (4) |
475 | 65 |
def eval(t: Tip) : Int = t match { |
66 |
case Correct => 10 |
|
67 |
case Present => 1 |
|
68 |
case Absent => 0 |
|
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 |
|
475 | 71 |
def iscore(secret: String, word: String) : Int = (for(current <- score(secret, word)) yield eval(current)).sum |
72 |
||
439 | 73 |
|
74 |
//iscore("chess", "caves") // => 21 |
|
75 |
//iscore("chess", "swiss") // => 20 |
|
76 |
||
475 | 77 |
// (5) t |
463 | 78 |
def lowest(secrets: List[String], word: String, current: Int, acc: List[String]) : List[String] = { |
475 | 79 |
if (secrets.isEmpty) acc |
80 |
else { |
|
81 |
val score = iscore(secrets.head, word) |
|
82 |
||
83 |
if (score == current) lowest(secrets.tail, word, current, secrets.head :: acc) |
|
84 |
else if (score < current) lowest(secrets.tail, word, score, List(secrets.head)) |
|
85 |
else lowest(secrets.tail, word, current, acc) |
|
439 | 86 |
} |
435
fda7c39f3b6a
updated wordle testing
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
87 |
} |
fda7c39f3b6a
updated wordle testing
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
88 |
|
475 | 89 |
def evil(secrets: List[String], word: String) = lowest(secrets, word, Int.MaxValue ,List()) |
435
fda7c39f3b6a
updated wordle testing
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
90 |
|
fda7c39f3b6a
updated wordle testing
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
91 |
|
439 | 92 |
//evil(secrets, "stent").length |
93 |
//evil(secrets, "hexes").length |
|
94 |
//evil(secrets, "horse").length |
|
95 |
//evil(secrets, "hoise").length |
|
96 |
//evil(secrets, "house").length |
|
435
fda7c39f3b6a
updated wordle testing
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
97 |
|
475 | 98 |
// (6) t |
435
fda7c39f3b6a
updated wordle testing
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
99 |
def frequencies(secrets: List[String]) : Map[Char, Double] = { |
475 | 100 |
val occurrences = secrets.mkString.groupBy(identity).mapValues(_.length) |
101 |
val all_letters = secrets.mkString.length.toDouble |
|
102 |
occurrences.map{ case (c, n) => c -> (1 - n/all_letters) }.toMap |
|
435
fda7c39f3b6a
updated wordle testing
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
103 |
} |
fda7c39f3b6a
updated wordle testing
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
104 |
|
475 | 105 |
// (7) |
106 |
def rank(frqs: Map[Char, Double], s: String) : Double = s.map(c => frqs(c)).sum |
|
435
fda7c39f3b6a
updated wordle testing
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
107 |
|
439 | 108 |
|
475 | 109 |
def ranked_evil(secrets: List[String], word: String): List[String] = { |
110 |
val frqs = frequencies(secrets) |
|
111 |
val ranked_secrets = evil(secrets, word).map(s => (s, rank(frqs, s))) |
|
112 |
List((ranked_secrets.sortWith(_._2 > _._2)).map(_._1).head ) |
|
113 |
} |
|
114 |
||
435
fda7c39f3b6a
updated wordle testing
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
115 |
} |
fda7c39f3b6a
updated wordle testing
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
116 |
|
439 | 117 |
|
118 |
||
119 |
||
120 |
||
435
fda7c39f3b6a
updated wordle testing
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff
changeset
|
121 |
|
439 | 122 |
// This template code is subject to copyright |
123 |
// by King's College London, 2022. Do not |
|
124 |
// make the template code public in any shape |
|
125 |
// or form, and do not exchange it with other |
|
126 |
// students under any circumstance. |