main_testing2/wordle.scala
author Christian Urban <christian.urban@kcl.ac.uk>
Sat, 11 Mar 2023 22:01:53 +0000
changeset 463 0315d9983cd0
parent 439 97594b9998a8
child 475 59e005dcf163
permissions -rw-r--r--
updated
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
439
97594b9998a8 updated jars
Christian Urban <christian.urban@kcl.ac.uk>
parents: 435
diff changeset
     1
// Main Part 3 about Evil Wordle
97594b9998a8 updated jars
Christian Urban <christian.urban@kcl.ac.uk>
parents: 435
diff changeset
     2
//===============================
435
fda7c39f3b6a updated wordle testing
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     3
439
97594b9998a8 updated jars
Christian Urban <christian.urban@kcl.ac.uk>
parents: 435
diff changeset
     4
97594b9998a8 updated jars
Christian Urban <christian.urban@kcl.ac.uk>
parents: 435
diff changeset
     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
97594b9998a8 updated jars
Christian Urban <christian.urban@kcl.ac.uk>
parents: 435
diff changeset
    10
// ADD YOUR CODE BELOW
97594b9998a8 updated jars
Christian Urban <christian.urban@kcl.ac.uk>
parents: 435
diff changeset
    11
//======================
463
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 439
diff changeset
    12
// def main(args: Array[String]): Unit = {
435
fda7c39f3b6a updated wordle testing
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    13
463
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 439
diff changeset
    14
//     val secrets = get_wordle_list("https://nms.kcl.ac.uk/christian.urban/wordle.txt")
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 439
diff changeset
    15
//     println(ranked_evil(secrets, "beats")== List("fuzzy"))
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 439
diff changeset
    16
//     println(ranked_evil(secrets, "vitae") == List("fuzzy"))
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 439
diff changeset
    17
//     println(ranked_evil(secrets, "bento") == List("fuzzy"))
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 439
diff changeset
    18
//     println(ranked_evil(secrets, "belts") == List("fuzzy"))
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 439
diff changeset
    19
//     println(ranked_evil(secrets, "abbey") == List("whizz"))
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 439
diff changeset
    20
//     println(ranked_evil(secrets, "afear") == List("buzzy"))
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 439
diff changeset
    21
//     println(ranked_evil(secrets, "zincy") == List("jugum"))
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 439
diff changeset
    22
//     println(ranked_evil(secrets, "zippy") == List("chuff"))
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 439
diff changeset
    23
   
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 439
diff changeset
    24
    
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 439
diff changeset
    25
//    }
439
97594b9998a8 updated jars
Christian Urban <christian.urban@kcl.ac.uk>
parents: 435
diff changeset
    26
97594b9998a8 updated jars
Christian Urban <christian.urban@kcl.ac.uk>
parents: 435
diff changeset
    27
//(1)
435
fda7c39f3b6a updated wordle testing
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    28
def get_wordle_list(url: String) : List[String] = {
463
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 439
diff changeset
    29
    try {
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 439
diff changeset
    30
        Source.fromURL(url).getLines.toList;
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 439
diff changeset
    31
    }
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 439
diff changeset
    32
    catch {
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 439
diff changeset
    33
        case _ : Throwable => List();
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 439
diff changeset
    34
    }    
435
fda7c39f3b6a updated wordle testing
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    35
}
fda7c39f3b6a updated wordle testing
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    36
// val secrets = get_wordle_list("https://nms.kcl.ac.uk/christian.urban/wordle.txt")
439
97594b9998a8 updated jars
Christian Urban <christian.urban@kcl.ac.uk>
parents: 435
diff changeset
    37
// secrets.length // => 12972
97594b9998a8 updated jars
Christian Urban <christian.urban@kcl.ac.uk>
parents: 435
diff changeset
    38
// secrets.filter(_.length != 5) // => Nil
435
fda7c39f3b6a updated wordle testing
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    39
439
97594b9998a8 updated jars
Christian Urban <christian.urban@kcl.ac.uk>
parents: 435
diff changeset
    40
//(2)
97594b9998a8 updated jars
Christian Urban <christian.urban@kcl.ac.uk>
parents: 435
diff changeset
    41
def removeN[A](xs: List[A], elem: A, n: Int) : List[A] = {
463
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 439
diff changeset
    42
    if (xs.isEmpty || n == 0 || !xs.contains(elem)){
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 439
diff changeset
    43
        xs
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 439
diff changeset
    44
    }
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 439
diff changeset
    45
    else{
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 439
diff changeset
    46
        val (newLst,newLst2) = xs.splitAt(xs.indexOf(elem))
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 439
diff changeset
    47
        removeN(newLst ++ newLst2.tail,elem, n-1)
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 439
diff changeset
    48
    }
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 439
diff changeset
    49
}
435
fda7c39f3b6a updated wordle testing
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    50
439
97594b9998a8 updated jars
Christian Urban <christian.urban@kcl.ac.uk>
parents: 435
diff changeset
    51
// removeN(List(1,2,3,2,1), 3, 1)  // => List(1, 2, 2, 1)
97594b9998a8 updated jars
Christian Urban <christian.urban@kcl.ac.uk>
parents: 435
diff changeset
    52
// removeN(List(1,2,3,2,1), 2, 1)  // => List(1, 3, 2, 1)
97594b9998a8 updated jars
Christian Urban <christian.urban@kcl.ac.uk>
parents: 435
diff changeset
    53
// removeN(List(1,2,3,2,1), 1, 1)  // => List(2, 3, 2, 1)
97594b9998a8 updated jars
Christian Urban <christian.urban@kcl.ac.uk>
parents: 435
diff changeset
    54
// 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
    55
439
97594b9998a8 updated jars
Christian Urban <christian.urban@kcl.ac.uk>
parents: 435
diff changeset
    56
// (3)
435
fda7c39f3b6a updated wordle testing
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    57
abstract class Tip
fda7c39f3b6a updated wordle testing
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    58
case object Absent extends Tip
fda7c39f3b6a updated wordle testing
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    59
case object Present extends Tip
fda7c39f3b6a updated wordle testing
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    60
case object Correct extends Tip
fda7c39f3b6a updated wordle testing
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    61
439
97594b9998a8 updated jars
Christian Urban <christian.urban@kcl.ac.uk>
parents: 435
diff changeset
    62
97594b9998a8 updated jars
Christian Urban <christian.urban@kcl.ac.uk>
parents: 435
diff changeset
    63
def pool(secret: String, word: String) : List[Char] = {
463
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 439
diff changeset
    64
    //print(secret.toList, word.toList)
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 439
diff changeset
    65
    val lst= ((0 to 4).map(x => {
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 439
diff changeset
    66
        if(!secret(x).equals(word(x))) Some(secret(x)) else None
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 439
diff changeset
    67
        
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 439
diff changeset
    68
        }).toList)
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 439
diff changeset
    69
    lst.flatten
435
fda7c39f3b6a updated wordle testing
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    70
}
fda7c39f3b6a updated wordle testing
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    71
463
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 439
diff changeset
    72
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 439
diff changeset
    73
def aux(secret: List[Char], word: List[Char], pool: List[Char]) : List[Tip] = {
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 439
diff changeset
    74
   
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 439
diff changeset
    75
    if (secret.length == 1){
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 439
diff changeset
    76
        if (secret.head == word.head){
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 439
diff changeset
    77
            List(Correct)
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 439
diff changeset
    78
        }
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 439
diff changeset
    79
        else if (pool.contains(word.head)){
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 439
diff changeset
    80
            List(Present) 
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 439
diff changeset
    81
        }
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 439
diff changeset
    82
        else {
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 439
diff changeset
    83
            List(Absent) 
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 439
diff changeset
    84
        }
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 439
diff changeset
    85
    }
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 439
diff changeset
    86
    else if (secret.head == word.head){
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 439
diff changeset
    87
        List(Correct) ++ aux(secret.tail, word.tail, pool)
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 439
diff changeset
    88
    }
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 439
diff changeset
    89
    else if (pool.contains(word.head)){
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 439
diff changeset
    90
        List(Present) ++ aux(secret.tail, word.tail, removeN(pool, word.head, 1))
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 439
diff changeset
    91
    }
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 439
diff changeset
    92
    else {
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 439
diff changeset
    93
        List(Absent) ++ aux(secret.tail, word.tail, pool)
435
fda7c39f3b6a updated wordle testing
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    94
    }
fda7c39f3b6a updated wordle testing
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    95
}
fda7c39f3b6a updated wordle testing
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    96
463
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 439
diff changeset
    97
def score(secret: String, word: String) : List[Tip] = {
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 439
diff changeset
    98
    aux(secret.toList,word.toList,pool(secret,word))
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 439
diff changeset
    99
}
435
fda7c39f3b6a updated wordle testing
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   100
fda7c39f3b6a updated wordle testing
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   101
439
97594b9998a8 updated jars
Christian Urban <christian.urban@kcl.ac.uk>
parents: 435
diff changeset
   102
// score("chess", "caves") // => List(Correct, Absent, Absent, Present, Correct)
97594b9998a8 updated jars
Christian Urban <christian.urban@kcl.ac.uk>
parents: 435
diff changeset
   103
// score("doses", "slide") // => List(Present, Absent, Absent, Present, Present)
97594b9998a8 updated jars
Christian Urban <christian.urban@kcl.ac.uk>
parents: 435
diff changeset
   104
// score("chess", "swiss") // => List(Absent, Absent, Absent, Correct, Correct)
97594b9998a8 updated jars
Christian Urban <christian.urban@kcl.ac.uk>
parents: 435
diff changeset
   105
// score("chess", "eexss") // => List(Present, Absent, Absent, Correct, Correct)
435
fda7c39f3b6a updated wordle testing
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   106
439
97594b9998a8 updated jars
Christian Urban <christian.urban@kcl.ac.uk>
parents: 435
diff changeset
   107
// (4)
463
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 439
diff changeset
   108
def eval(t: Tip) : Int = {
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 439
diff changeset
   109
    if (t == Correct) 10
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 439
diff changeset
   110
    else if (t == Present) 1
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 439
diff changeset
   111
    else 0
435
fda7c39f3b6a updated wordle testing
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   112
}
fda7c39f3b6a updated wordle testing
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   113
463
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 439
diff changeset
   114
def iscore(secret: String, word: String) : Int = {
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 439
diff changeset
   115
    val scoreList = score(secret,word)
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 439
diff changeset
   116
    scoreList.map(x =>(eval(x))).toList.sum
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 439
diff changeset
   117
}
439
97594b9998a8 updated jars
Christian Urban <christian.urban@kcl.ac.uk>
parents: 435
diff changeset
   118
97594b9998a8 updated jars
Christian Urban <christian.urban@kcl.ac.uk>
parents: 435
diff changeset
   119
//iscore("chess", "caves") // => 21
97594b9998a8 updated jars
Christian Urban <christian.urban@kcl.ac.uk>
parents: 435
diff changeset
   120
//iscore("chess", "swiss") // => 20
97594b9998a8 updated jars
Christian Urban <christian.urban@kcl.ac.uk>
parents: 435
diff changeset
   121
97594b9998a8 updated jars
Christian Urban <christian.urban@kcl.ac.uk>
parents: 435
diff changeset
   122
// (5)
463
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 439
diff changeset
   123
def lowest(secrets: List[String], word: String, current: Int, acc: List[String]) : List[String] = {
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 439
diff changeset
   124
    if(secrets.isEmpty) acc
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 439
diff changeset
   125
    else if (iscore(secrets.head,word)<current){
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 439
diff changeset
   126
        lowest(secrets.tail, word, iscore(secrets.head,word), List(secrets.head))
439
97594b9998a8 updated jars
Christian Urban <christian.urban@kcl.ac.uk>
parents: 435
diff changeset
   127
    }
463
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 439
diff changeset
   128
    else if (iscore(secrets.head,word)==current){
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 439
diff changeset
   129
        lowest(secrets.tail, word, current, acc :+ secrets.head)
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 439
diff changeset
   130
    }
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 439
diff changeset
   131
    else {
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 439
diff changeset
   132
        lowest(secrets.tail, word, current, acc)
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 439
diff changeset
   133
    }
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 439
diff changeset
   134
435
fda7c39f3b6a updated wordle testing
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   135
}
fda7c39f3b6a updated wordle testing
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   136
463
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 439
diff changeset
   137
def evil(secrets: List[String], word: String) = {
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 439
diff changeset
   138
    lowest(secrets, word, 100, List())
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 439
diff changeset
   139
}
435
fda7c39f3b6a updated wordle testing
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   140
fda7c39f3b6a updated wordle testing
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   141
439
97594b9998a8 updated jars
Christian Urban <christian.urban@kcl.ac.uk>
parents: 435
diff changeset
   142
//evil(secrets, "stent").length
97594b9998a8 updated jars
Christian Urban <christian.urban@kcl.ac.uk>
parents: 435
diff changeset
   143
//evil(secrets, "hexes").length
97594b9998a8 updated jars
Christian Urban <christian.urban@kcl.ac.uk>
parents: 435
diff changeset
   144
//evil(secrets, "horse").length
97594b9998a8 updated jars
Christian Urban <christian.urban@kcl.ac.uk>
parents: 435
diff changeset
   145
//evil(secrets, "hoise").length
97594b9998a8 updated jars
Christian Urban <christian.urban@kcl.ac.uk>
parents: 435
diff changeset
   146
//evil(secrets, "house").length
435
fda7c39f3b6a updated wordle testing
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   147
439
97594b9998a8 updated jars
Christian Urban <christian.urban@kcl.ac.uk>
parents: 435
diff changeset
   148
// (6)
435
fda7c39f3b6a updated wordle testing
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   149
def frequencies(secrets: List[String]) : Map[Char, Double] = {
463
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 439
diff changeset
   150
    val totalChar = secrets.flatMap(_.toList).size.toDouble
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 439
diff changeset
   151
    val freqMap = secrets.flatMap(_.toList).groupBy(identity)
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 439
diff changeset
   152
    freqMap.map(x => (x._1, (1-(x._2.size.toDouble/ totalChar))))
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 439
diff changeset
   153
435
fda7c39f3b6a updated wordle testing
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   154
}
fda7c39f3b6a updated wordle testing
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   155
439
97594b9998a8 updated jars
Christian Urban <christian.urban@kcl.ac.uk>
parents: 435
diff changeset
   156
// (7)
435
fda7c39f3b6a updated wordle testing
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   157
def rank(frqs: Map[Char, Double], s: String) = {
463
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 439
diff changeset
   158
    s.map(x => (frqs(x))).toList.sum
435
fda7c39f3b6a updated wordle testing
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   159
}
fda7c39f3b6a updated wordle testing
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   160
463
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 439
diff changeset
   161
def ranked_evil(secrets: List[String], word: String) : List[String]= {
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 439
diff changeset
   162
    val evilWords = evil(secrets,word)
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 439
diff changeset
   163
    val returnVal = evilWords.map(x => (x, rank(frequencies(secrets),x))).toMap.maxBy(_._2)._1
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 439
diff changeset
   164
    List(returnVal)
435
fda7c39f3b6a updated wordle testing
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   165
}
fda7c39f3b6a updated wordle testing
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   166
439
97594b9998a8 updated jars
Christian Urban <christian.urban@kcl.ac.uk>
parents: 435
diff changeset
   167
435
fda7c39f3b6a updated wordle testing
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   168
}
fda7c39f3b6a updated wordle testing
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   169
439
97594b9998a8 updated jars
Christian Urban <christian.urban@kcl.ac.uk>
parents: 435
diff changeset
   170
97594b9998a8 updated jars
Christian Urban <christian.urban@kcl.ac.uk>
parents: 435
diff changeset
   171
97594b9998a8 updated jars
Christian Urban <christian.urban@kcl.ac.uk>
parents: 435
diff changeset
   172
97594b9998a8 updated jars
Christian Urban <christian.urban@kcl.ac.uk>
parents: 435
diff changeset
   173
435
fda7c39f3b6a updated wordle testing
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   174
439
97594b9998a8 updated jars
Christian Urban <christian.urban@kcl.ac.uk>
parents: 435
diff changeset
   175
// This template code is subject to copyright 
97594b9998a8 updated jars
Christian Urban <christian.urban@kcl.ac.uk>
parents: 435
diff changeset
   176
// by King's College London, 2022. Do not 
97594b9998a8 updated jars
Christian Urban <christian.urban@kcl.ac.uk>
parents: 435
diff changeset
   177
// make the template code public in any shape 
97594b9998a8 updated jars
Christian Urban <christian.urban@kcl.ac.uk>
parents: 435
diff changeset
   178
// or form, and do not exchange it with other 
97594b9998a8 updated jars
Christian Urban <christian.urban@kcl.ac.uk>
parents: 435
diff changeset
   179
// students under any circumstance.