| author | Christian Urban <christian.urban@kcl.ac.uk> | 
| Wed, 09 Nov 2022 18:44:40 +0000 | |
| changeset 435 | 5d06cbea2000 | 
| parent 432 | 87e487ccbd7c | 
| child 436 | 2f17f233cddc | 
| permissions | -rw-r--r-- | 
| 432 
87e487ccbd7c
updated wordle testing
 Christian Urban <christian.urban@kcl.ac.uk> parents: diff
changeset | 1 | // Resit Part about an Evil Wordle Clone | 
| 
87e487ccbd7c
updated wordle testing
 Christian Urban <christian.urban@kcl.ac.uk> parents: diff
changeset | 2 | //========================================== | 
| 
87e487ccbd7c
updated wordle testing
 Christian Urban <christian.urban@kcl.ac.uk> parents: diff
changeset | 3 | // | 
| 
87e487ccbd7c
updated wordle testing
 Christian Urban <christian.urban@kcl.ac.uk> parents: diff
changeset | 4 | // Task description are on KEATS | 
| 
87e487ccbd7c
updated wordle testing
 Christian Urban <christian.urban@kcl.ac.uk> parents: diff
changeset | 5 | // | 
| 
87e487ccbd7c
updated wordle testing
 Christian Urban <christian.urban@kcl.ac.uk> parents: diff
changeset | 6 | // Make sure you use Scala Version 2.13 (not Scala 3) | 
| 
87e487ccbd7c
updated wordle testing
 Christian Urban <christian.urban@kcl.ac.uk> parents: diff
changeset | 7 | // | 
| 
87e487ccbd7c
updated wordle testing
 Christian Urban <christian.urban@kcl.ac.uk> parents: diff
changeset | 8 | // Upload your submision to OneSpace and give Senir and Christian | 
| 
87e487ccbd7c
updated wordle testing
 Christian Urban <christian.urban@kcl.ac.uk> parents: diff
changeset | 9 | // write access to the folder. The folder should have you email | 
| 
87e487ccbd7c
updated wordle testing
 Christian Urban <christian.urban@kcl.ac.uk> parents: diff
changeset | 10 | // address as name. Give us write permission by July 25th such | 
| 
87e487ccbd7c
updated wordle testing
 Christian Urban <christian.urban@kcl.ac.uk> parents: diff
changeset | 11 | // that we can download your solution by the closing date August 4th. | 
| 
87e487ccbd7c
updated wordle testing
 Christian Urban <christian.urban@kcl.ac.uk> parents: diff
changeset | 12 | |
| 
87e487ccbd7c
updated wordle testing
 Christian Urban <christian.urban@kcl.ac.uk> parents: diff
changeset | 13 | object Resit {
 | 
| 
87e487ccbd7c
updated wordle testing
 Christian Urban <christian.urban@kcl.ac.uk> parents: diff
changeset | 14 | |
| 
87e487ccbd7c
updated wordle testing
 Christian Urban <christian.urban@kcl.ac.uk> parents: diff
changeset | 15 | import io.Source | 
| 
87e487ccbd7c
updated wordle testing
 Christian Urban <christian.urban@kcl.ac.uk> parents: diff
changeset | 16 | import scala.util._ | 
| 
87e487ccbd7c
updated wordle testing
 Christian Urban <christian.urban@kcl.ac.uk> parents: diff
changeset | 17 | |
| 
87e487ccbd7c
updated wordle testing
 Christian Urban <christian.urban@kcl.ac.uk> parents: diff
changeset | 18 | //============================= | 
| 
87e487ccbd7c
updated wordle testing
 Christian Urban <christian.urban@kcl.ac.uk> parents: diff
changeset | 19 | // Task 1 [0.25 Marks] | 
| 
87e487ccbd7c
updated wordle testing
 Christian Urban <christian.urban@kcl.ac.uk> parents: diff
changeset | 20 | |
| 
87e487ccbd7c
updated wordle testing
 Christian Urban <christian.urban@kcl.ac.uk> parents: diff
changeset | 21 | def get_wordle_list(url: String) : List[String] = {
 | 
| 
87e487ccbd7c
updated wordle testing
 Christian Urban <christian.urban@kcl.ac.uk> parents: diff
changeset | 22 |     try{
 | 
| 
87e487ccbd7c
updated wordle testing
 Christian Urban <christian.urban@kcl.ac.uk> parents: diff
changeset | 23 | val raw = Source.fromURL(url).mkString | 
| 
87e487ccbd7c
updated wordle testing
 Christian Urban <christian.urban@kcl.ac.uk> parents: diff
changeset | 24 | 		raw.split("\n").toList
 | 
| 
87e487ccbd7c
updated wordle testing
 Christian Urban <christian.urban@kcl.ac.uk> parents: diff
changeset | 25 | } | 
| 
87e487ccbd7c
updated wordle testing
 Christian Urban <christian.urban@kcl.ac.uk> parents: diff
changeset | 26 | 	catch {
 | 
| 
87e487ccbd7c
updated wordle testing
 Christian Urban <christian.urban@kcl.ac.uk> parents: diff
changeset | 27 | case e : Exception => List[String]() | 
| 
87e487ccbd7c
updated wordle testing
 Christian Urban <christian.urban@kcl.ac.uk> parents: diff
changeset | 28 | } | 
| 
87e487ccbd7c
updated wordle testing
 Christian Urban <christian.urban@kcl.ac.uk> parents: diff
changeset | 29 | } | 
| 
87e487ccbd7c
updated wordle testing
 Christian Urban <christian.urban@kcl.ac.uk> parents: diff
changeset | 30 | |
| 
87e487ccbd7c
updated wordle testing
 Christian Urban <christian.urban@kcl.ac.uk> parents: diff
changeset | 31 | |
| 
87e487ccbd7c
updated wordle testing
 Christian Urban <christian.urban@kcl.ac.uk> parents: diff
changeset | 32 | // val secrets = get_wordle_list("https://nms.kcl.ac.uk/christian.urban/wordle.txt")
 | 
| 
87e487ccbd7c
updated wordle testing
 Christian Urban <christian.urban@kcl.ac.uk> parents: diff
changeset | 33 | // secrets.length => 12972 | 
| 
87e487ccbd7c
updated wordle testing
 Christian Urban <christian.urban@kcl.ac.uk> parents: diff
changeset | 34 | // secrets.filter(_.length != 5) => Nil | 
| 
87e487ccbd7c
updated wordle testing
 Christian Urban <christian.urban@kcl.ac.uk> parents: diff
changeset | 35 | |
| 
87e487ccbd7c
updated wordle testing
 Christian Urban <christian.urban@kcl.ac.uk> parents: diff
changeset | 36 | //============================= | 
| 
87e487ccbd7c
updated wordle testing
 Christian Urban <christian.urban@kcl.ac.uk> parents: diff
changeset | 37 | // Task 2 [0.25 Marks] | 
| 
87e487ccbd7c
updated wordle testing
 Christian Urban <christian.urban@kcl.ac.uk> parents: diff
changeset | 38 | |
| 
87e487ccbd7c
updated wordle testing
 Christian Urban <christian.urban@kcl.ac.uk> parents: diff
changeset | 39 | def removeOne[A](xs: List[A], elem: A) : List[A] = {
 | 
| 
87e487ccbd7c
updated wordle testing
 Christian Urban <christian.urban@kcl.ac.uk> parents: diff
changeset | 40 | val index = xs.indexOf(elem) | 
| 
87e487ccbd7c
updated wordle testing
 Christian Urban <christian.urban@kcl.ac.uk> parents: diff
changeset | 41 | val first = xs.take(index) | 
| 
87e487ccbd7c
updated wordle testing
 Christian Urban <christian.urban@kcl.ac.uk> parents: diff
changeset | 42 | val second = xs.drop(index + 1) | 
| 
87e487ccbd7c
updated wordle testing
 Christian Urban <christian.urban@kcl.ac.uk> parents: diff
changeset | 43 | first ::: second | 
| 
87e487ccbd7c
updated wordle testing
 Christian Urban <christian.urban@kcl.ac.uk> parents: diff
changeset | 44 | } | 
| 
87e487ccbd7c
updated wordle testing
 Christian Urban <christian.urban@kcl.ac.uk> parents: diff
changeset | 45 | |
| 
87e487ccbd7c
updated wordle testing
 Christian Urban <christian.urban@kcl.ac.uk> parents: diff
changeset | 46 | // removeOne(List(1,2,3,2,1), 3) => List(1, 2, 2, 1) | 
| 
87e487ccbd7c
updated wordle testing
 Christian Urban <christian.urban@kcl.ac.uk> parents: diff
changeset | 47 | // removeOne(List(1,2,3,2,1), 2) => List(1, 3, 2, 1) | 
| 
87e487ccbd7c
updated wordle testing
 Christian Urban <christian.urban@kcl.ac.uk> parents: diff
changeset | 48 | // removeOne(List(1,2,3,2,1), 1) => List(2, 3, 2, 1) | 
| 
87e487ccbd7c
updated wordle testing
 Christian Urban <christian.urban@kcl.ac.uk> parents: diff
changeset | 49 | // removeOne(List(1,2,3,2,1), 0) => List(1, 2, 3, 2, 1) | 
| 
87e487ccbd7c
updated wordle testing
 Christian Urban <christian.urban@kcl.ac.uk> parents: diff
changeset | 50 | |
| 
87e487ccbd7c
updated wordle testing
 Christian Urban <christian.urban@kcl.ac.uk> parents: diff
changeset | 51 | |
| 
87e487ccbd7c
updated wordle testing
 Christian Urban <christian.urban@kcl.ac.uk> parents: diff
changeset | 52 | //============================= | 
| 
87e487ccbd7c
updated wordle testing
 Christian Urban <christian.urban@kcl.ac.uk> parents: diff
changeset | 53 | // Task 3 [1.5 Marks] | 
| 
87e487ccbd7c
updated wordle testing
 Christian Urban <christian.urban@kcl.ac.uk> parents: diff
changeset | 54 | |
| 
87e487ccbd7c
updated wordle testing
 Christian Urban <christian.urban@kcl.ac.uk> parents: diff
changeset | 55 | abstract class Tip | 
| 
87e487ccbd7c
updated wordle testing
 Christian Urban <christian.urban@kcl.ac.uk> parents: diff
changeset | 56 | case object Absent extends Tip | 
| 
87e487ccbd7c
updated wordle testing
 Christian Urban <christian.urban@kcl.ac.uk> parents: diff
changeset | 57 | case object Present extends Tip | 
| 
87e487ccbd7c
updated wordle testing
 Christian Urban <christian.urban@kcl.ac.uk> parents: diff
changeset | 58 | case object Correct extends Tip | 
| 
87e487ccbd7c
updated wordle testing
 Christian Urban <christian.urban@kcl.ac.uk> parents: diff
changeset | 59 | |
| 
87e487ccbd7c
updated wordle testing
 Christian Urban <christian.urban@kcl.ac.uk> parents: diff
changeset | 60 | def pool(secret: String, word: String) : List[Char]= {
 | 
| 
87e487ccbd7c
updated wordle testing
 Christian Urban <christian.urban@kcl.ac.uk> parents: diff
changeset | 61 | for (i <- (0 to 4).toList | 
| 
87e487ccbd7c
updated wordle testing
 Christian Urban <christian.urban@kcl.ac.uk> parents: diff
changeset | 62 | if secret(i) != word(i)) | 
| 
87e487ccbd7c
updated wordle testing
 Christian Urban <christian.urban@kcl.ac.uk> parents: diff
changeset | 63 | yield secret(i) | 
| 
87e487ccbd7c
updated wordle testing
 Christian Urban <christian.urban@kcl.ac.uk> parents: diff
changeset | 64 | } | 
| 
87e487ccbd7c
updated wordle testing
 Christian Urban <christian.urban@kcl.ac.uk> parents: diff
changeset | 65 | |
| 
87e487ccbd7c
updated wordle testing
 Christian Urban <christian.urban@kcl.ac.uk> parents: diff
changeset | 66 | def aux(secret: List[Char], word: List[Char], pool: List[Char]) : List[Tip] = (secret, word) match {
 | 
| 
87e487ccbd7c
updated wordle testing
 Christian Urban <christian.urban@kcl.ac.uk> parents: diff
changeset | 67 | case (Nil, Nil) => Nil | 
| 
87e487ccbd7c
updated wordle testing
 Christian Urban <christian.urban@kcl.ac.uk> parents: diff
changeset | 68 |     case (s::srest, w::wrest) => {
 | 
| 
87e487ccbd7c
updated wordle testing
 Christian Urban <christian.urban@kcl.ac.uk> parents: diff
changeset | 69 | if (s == w) Correct::aux(srest, wrest, pool) | 
| 
87e487ccbd7c
updated wordle testing
 Christian Urban <christian.urban@kcl.ac.uk> parents: diff
changeset | 70 | else if (pool.contains(w)) Present::aux(srest, wrest, removeOne(pool, w)) | 
| 
87e487ccbd7c
updated wordle testing
 Christian Urban <christian.urban@kcl.ac.uk> parents: diff
changeset | 71 | else Absent::aux(srest, wrest, pool) | 
| 
87e487ccbd7c
updated wordle testing
 Christian Urban <christian.urban@kcl.ac.uk> parents: diff
changeset | 72 | } | 
| 
87e487ccbd7c
updated wordle testing
 Christian Urban <christian.urban@kcl.ac.uk> parents: diff
changeset | 73 | } | 
| 
87e487ccbd7c
updated wordle testing
 Christian Urban <christian.urban@kcl.ac.uk> parents: diff
changeset | 74 | |
| 
87e487ccbd7c
updated wordle testing
 Christian Urban <christian.urban@kcl.ac.uk> parents: diff
changeset | 75 | def score(secret: String, word: String) : List[Tip] = | 
| 
87e487ccbd7c
updated wordle testing
 Christian Urban <christian.urban@kcl.ac.uk> parents: diff
changeset | 76 | aux(secret.toList, word.toList, pool(secret, word)) | 
| 
87e487ccbd7c
updated wordle testing
 Christian Urban <christian.urban@kcl.ac.uk> parents: diff
changeset | 77 | |
| 
87e487ccbd7c
updated wordle testing
 Christian Urban <christian.urban@kcl.ac.uk> parents: diff
changeset | 78 | /* | 
| 
87e487ccbd7c
updated wordle testing
 Christian Urban <christian.urban@kcl.ac.uk> parents: diff
changeset | 79 | def pool(secret: String, word: String) : List[Char] = {
 | 
| 
87e487ccbd7c
updated wordle testing
 Christian Urban <christian.urban@kcl.ac.uk> parents: diff
changeset | 80 | 	(secret zip word).collect {
 | 
| 
87e487ccbd7c
updated wordle testing
 Christian Urban <christian.urban@kcl.ac.uk> parents: diff
changeset | 81 | case (c1, c2) if c1 != c2 => s"$c1" | 
| 
87e487ccbd7c
updated wordle testing
 Christian Urban <christian.urban@kcl.ac.uk> parents: diff
changeset | 82 | }.toList.flatten | 
| 
87e487ccbd7c
updated wordle testing
 Christian Urban <christian.urban@kcl.ac.uk> parents: diff
changeset | 83 | } | 
| 
87e487ccbd7c
updated wordle testing
 Christian Urban <christian.urban@kcl.ac.uk> parents: diff
changeset | 84 | |
| 
87e487ccbd7c
updated wordle testing
 Christian Urban <christian.urban@kcl.ac.uk> parents: diff
changeset | 85 | def aux2(secret: String, word: String, pool: List[Char], result: List[Tip]) : List[Tip] = {
 | 
| 
87e487ccbd7c
updated wordle testing
 Christian Urban <christian.urban@kcl.ac.uk> parents: diff
changeset | 86 |     if (word.length == 0){
 | 
| 
87e487ccbd7c
updated wordle testing
 Christian Urban <christian.urban@kcl.ac.uk> parents: diff
changeset | 87 | result.reverse | 
| 
87e487ccbd7c
updated wordle testing
 Christian Urban <christian.urban@kcl.ac.uk> parents: diff
changeset | 88 | } | 
| 
87e487ccbd7c
updated wordle testing
 Christian Urban <christian.urban@kcl.ac.uk> parents: diff
changeset | 89 |     else{
 | 
| 
87e487ccbd7c
updated wordle testing
 Christian Urban <christian.urban@kcl.ac.uk> parents: diff
changeset | 90 | 		if (word.take(1) == secret.take(1)) {
 | 
| 
87e487ccbd7c
updated wordle testing
 Christian Urban <christian.urban@kcl.ac.uk> parents: diff
changeset | 91 | val updated = Correct :: result | 
| 
87e487ccbd7c
updated wordle testing
 Christian Urban <christian.urban@kcl.ac.uk> parents: diff
changeset | 92 | aux2(secret.drop(1), word.drop(1), pool, updated) | 
| 
87e487ccbd7c
updated wordle testing
 Christian Urban <christian.urban@kcl.ac.uk> parents: diff
changeset | 93 | } | 
| 
87e487ccbd7c
updated wordle testing
 Christian Urban <christian.urban@kcl.ac.uk> parents: diff
changeset | 94 | 		else {
 | 
| 
87e487ccbd7c
updated wordle testing
 Christian Urban <christian.urban@kcl.ac.uk> parents: diff
changeset | 95 | 			if(pool.contains(word.take(1)(0))){
 | 
| 
87e487ccbd7c
updated wordle testing
 Christian Urban <christian.urban@kcl.ac.uk> parents: diff
changeset | 96 | val updated = Present :: result | 
| 
87e487ccbd7c
updated wordle testing
 Christian Urban <christian.urban@kcl.ac.uk> parents: diff
changeset | 97 | val removed = removeOne(pool, word.take(1)(0)).mkString.toList | 
| 
87e487ccbd7c
updated wordle testing
 Christian Urban <christian.urban@kcl.ac.uk> parents: diff
changeset | 98 | aux2(secret.drop(1), word.drop(1), removed, updated) | 
| 
87e487ccbd7c
updated wordle testing
 Christian Urban <christian.urban@kcl.ac.uk> parents: diff
changeset | 99 | } | 
| 
87e487ccbd7c
updated wordle testing
 Christian Urban <christian.urban@kcl.ac.uk> parents: diff
changeset | 100 | 			else{
 | 
| 
87e487ccbd7c
updated wordle testing
 Christian Urban <christian.urban@kcl.ac.uk> parents: diff
changeset | 101 | val updated = Absent :: result | 
| 
87e487ccbd7c
updated wordle testing
 Christian Urban <christian.urban@kcl.ac.uk> parents: diff
changeset | 102 | aux2(secret.drop(1), word.drop(1), pool, updated) | 
| 
87e487ccbd7c
updated wordle testing
 Christian Urban <christian.urban@kcl.ac.uk> parents: diff
changeset | 103 | } | 
| 
87e487ccbd7c
updated wordle testing
 Christian Urban <christian.urban@kcl.ac.uk> parents: diff
changeset | 104 | } | 
| 
87e487ccbd7c
updated wordle testing
 Christian Urban <christian.urban@kcl.ac.uk> parents: diff
changeset | 105 | } | 
| 
87e487ccbd7c
updated wordle testing
 Christian Urban <christian.urban@kcl.ac.uk> parents: diff
changeset | 106 | } | 
| 
87e487ccbd7c
updated wordle testing
 Christian Urban <christian.urban@kcl.ac.uk> parents: diff
changeset | 107 | |
| 
87e487ccbd7c
updated wordle testing
 Christian Urban <christian.urban@kcl.ac.uk> parents: diff
changeset | 108 | def aux(secret: List[Char], word: List[Char], pool: List[Char]) : List[Tip] = {
 | 
| 
87e487ccbd7c
updated wordle testing
 Christian Urban <christian.urban@kcl.ac.uk> parents: diff
changeset | 109 | aux2(secret.mkString, word.mkString, pool, List()) | 
| 
87e487ccbd7c
updated wordle testing
 Christian Urban <christian.urban@kcl.ac.uk> parents: diff
changeset | 110 | } | 
| 
87e487ccbd7c
updated wordle testing
 Christian Urban <christian.urban@kcl.ac.uk> parents: diff
changeset | 111 | |
| 
87e487ccbd7c
updated wordle testing
 Christian Urban <christian.urban@kcl.ac.uk> parents: diff
changeset | 112 | def score(secret: String, word: String) : List[Tip] = {
 | 
| 
87e487ccbd7c
updated wordle testing
 Christian Urban <christian.urban@kcl.ac.uk> parents: diff
changeset | 113 | aux(secret.toList, word.toList, pool(secret, word)) | 
| 
87e487ccbd7c
updated wordle testing
 Christian Urban <christian.urban@kcl.ac.uk> parents: diff
changeset | 114 | } | 
| 
87e487ccbd7c
updated wordle testing
 Christian Urban <christian.urban@kcl.ac.uk> parents: diff
changeset | 115 | */ | 
| 
87e487ccbd7c
updated wordle testing
 Christian Urban <christian.urban@kcl.ac.uk> parents: diff
changeset | 116 | |
| 
87e487ccbd7c
updated wordle testing
 Christian Urban <christian.urban@kcl.ac.uk> parents: diff
changeset | 117 | // score("chess", "caves")
 | 
| 
87e487ccbd7c
updated wordle testing
 Christian Urban <christian.urban@kcl.ac.uk> parents: diff
changeset | 118 | // score("doses", "slide")
 | 
| 
87e487ccbd7c
updated wordle testing
 Christian Urban <christian.urban@kcl.ac.uk> parents: diff
changeset | 119 | // score("chess", "swiss")
 | 
| 
87e487ccbd7c
updated wordle testing
 Christian Urban <christian.urban@kcl.ac.uk> parents: diff
changeset | 120 | // score("chess", "eexss")
 | 
| 
87e487ccbd7c
updated wordle testing
 Christian Urban <christian.urban@kcl.ac.uk> parents: diff
changeset | 121 | |
| 
87e487ccbd7c
updated wordle testing
 Christian Urban <christian.urban@kcl.ac.uk> parents: diff
changeset | 122 | |
| 
87e487ccbd7c
updated wordle testing
 Christian Urban <christian.urban@kcl.ac.uk> parents: diff
changeset | 123 | //============================= | 
| 
87e487ccbd7c
updated wordle testing
 Christian Urban <christian.urban@kcl.ac.uk> parents: diff
changeset | 124 | // Task 4 [0.5 Marks] | 
| 
87e487ccbd7c
updated wordle testing
 Christian Urban <christian.urban@kcl.ac.uk> parents: diff
changeset | 125 | |
| 
87e487ccbd7c
updated wordle testing
 Christian Urban <christian.urban@kcl.ac.uk> parents: diff
changeset | 126 | def eval(t: Tip) : Int = t match {
 | 
| 
87e487ccbd7c
updated wordle testing
 Christian Urban <christian.urban@kcl.ac.uk> parents: diff
changeset | 127 | case Correct => 10 | 
| 
87e487ccbd7c
updated wordle testing
 Christian Urban <christian.urban@kcl.ac.uk> parents: diff
changeset | 128 | case Present => 1 | 
| 
87e487ccbd7c
updated wordle testing
 Christian Urban <christian.urban@kcl.ac.uk> parents: diff
changeset | 129 | case Absent => 0 | 
| 
87e487ccbd7c
updated wordle testing
 Christian Urban <christian.urban@kcl.ac.uk> parents: diff
changeset | 130 | } | 
| 
87e487ccbd7c
updated wordle testing
 Christian Urban <christian.urban@kcl.ac.uk> parents: diff
changeset | 131 | |
| 
87e487ccbd7c
updated wordle testing
 Christian Urban <christian.urban@kcl.ac.uk> parents: diff
changeset | 132 | def iscore(secret: String, word: String) : Int = {
 | 
| 
87e487ccbd7c
updated wordle testing
 Christian Urban <christian.urban@kcl.ac.uk> parents: diff
changeset | 133 | val list = score(secret, word) | 
| 
87e487ccbd7c
updated wordle testing
 Christian Urban <christian.urban@kcl.ac.uk> parents: diff
changeset | 134 | val values = list.map(n => eval(n)) | 
| 
87e487ccbd7c
updated wordle testing
 Christian Urban <christian.urban@kcl.ac.uk> parents: diff
changeset | 135 | values.sum | 
| 
87e487ccbd7c
updated wordle testing
 Christian Urban <christian.urban@kcl.ac.uk> parents: diff
changeset | 136 | } | 
| 
87e487ccbd7c
updated wordle testing
 Christian Urban <christian.urban@kcl.ac.uk> parents: diff
changeset | 137 | |
| 
87e487ccbd7c
updated wordle testing
 Christian Urban <christian.urban@kcl.ac.uk> parents: diff
changeset | 138 | // iscore("chess", "caves")
 | 
| 
87e487ccbd7c
updated wordle testing
 Christian Urban <christian.urban@kcl.ac.uk> parents: diff
changeset | 139 | // iscore("doses", "slide")
 | 
| 
87e487ccbd7c
updated wordle testing
 Christian Urban <christian.urban@kcl.ac.uk> parents: diff
changeset | 140 | // iscore("chess", "swiss")
 | 
| 
87e487ccbd7c
updated wordle testing
 Christian Urban <christian.urban@kcl.ac.uk> parents: diff
changeset | 141 | // iscore("chess", "eexss")
 | 
| 
87e487ccbd7c
updated wordle testing
 Christian Urban <christian.urban@kcl.ac.uk> parents: diff
changeset | 142 | |
| 
87e487ccbd7c
updated wordle testing
 Christian Urban <christian.urban@kcl.ac.uk> parents: diff
changeset | 143 | //============================= | 
| 
87e487ccbd7c
updated wordle testing
 Christian Urban <christian.urban@kcl.ac.uk> parents: diff
changeset | 144 | // Task 5 [1.5 Mark] | 
| 
87e487ccbd7c
updated wordle testing
 Christian Urban <christian.urban@kcl.ac.uk> parents: diff
changeset | 145 | |
| 
87e487ccbd7c
updated wordle testing
 Christian Urban <christian.urban@kcl.ac.uk> parents: diff
changeset | 146 | def lowest(secrets: List[String], word: String, current: Int, acc: List[String]) : List[String] = {
 | 
| 
87e487ccbd7c
updated wordle testing
 Christian Urban <christian.urban@kcl.ac.uk> parents: diff
changeset | 147 | val mapped = secrets.map(x => (x, iscore(x, word))) | 
| 
87e487ccbd7c
updated wordle testing
 Christian Urban <christian.urban@kcl.ac.uk> parents: diff
changeset | 148 | val min = mapped.minBy(_._2)._2 | 
| 
87e487ccbd7c
updated wordle testing
 Christian Urban <christian.urban@kcl.ac.uk> parents: diff
changeset | 149 | val filt = mapped.filter(_._2 == min) | 
| 
87e487ccbd7c
updated wordle testing
 Christian Urban <christian.urban@kcl.ac.uk> parents: diff
changeset | 150 | filt.map(x => x._1) | 
| 
87e487ccbd7c
updated wordle testing
 Christian Urban <christian.urban@kcl.ac.uk> parents: diff
changeset | 151 | } | 
| 
87e487ccbd7c
updated wordle testing
 Christian Urban <christian.urban@kcl.ac.uk> parents: diff
changeset | 152 | |
| 
87e487ccbd7c
updated wordle testing
 Christian Urban <christian.urban@kcl.ac.uk> parents: diff
changeset | 153 | def evil(secrets: List[String], word: String) : List[String] = {
 | 
| 
87e487ccbd7c
updated wordle testing
 Christian Urban <christian.urban@kcl.ac.uk> parents: diff
changeset | 154 | lowest(secrets, word, Int.MaxValue, List()) | 
| 
87e487ccbd7c
updated wordle testing
 Christian Urban <christian.urban@kcl.ac.uk> parents: diff
changeset | 155 | } | 
| 
87e487ccbd7c
updated wordle testing
 Christian Urban <christian.urban@kcl.ac.uk> parents: diff
changeset | 156 | |
| 
87e487ccbd7c
updated wordle testing
 Christian Urban <christian.urban@kcl.ac.uk> parents: diff
changeset | 157 | //evil(secrets, "stent").length | 
| 
87e487ccbd7c
updated wordle testing
 Christian Urban <christian.urban@kcl.ac.uk> parents: diff
changeset | 158 | //evil(secrets, "hexes").length | 
| 
87e487ccbd7c
updated wordle testing
 Christian Urban <christian.urban@kcl.ac.uk> parents: diff
changeset | 159 | //evil(secrets, "horse").length | 
| 
87e487ccbd7c
updated wordle testing
 Christian Urban <christian.urban@kcl.ac.uk> parents: diff
changeset | 160 | //evil(secrets, "hoise").length | 
| 
87e487ccbd7c
updated wordle testing
 Christian Urban <christian.urban@kcl.ac.uk> parents: diff
changeset | 161 | //evil(secrets, "house").length | 
| 
87e487ccbd7c
updated wordle testing
 Christian Urban <christian.urban@kcl.ac.uk> parents: diff
changeset | 162 | |
| 
87e487ccbd7c
updated wordle testing
 Christian Urban <christian.urban@kcl.ac.uk> parents: diff
changeset | 163 | |
| 
87e487ccbd7c
updated wordle testing
 Christian Urban <christian.urban@kcl.ac.uk> parents: diff
changeset | 164 | //============================= | 
| 
87e487ccbd7c
updated wordle testing
 Christian Urban <christian.urban@kcl.ac.uk> parents: diff
changeset | 165 | // Task 6 [1 Mark] | 
| 
87e487ccbd7c
updated wordle testing
 Christian Urban <christian.urban@kcl.ac.uk> parents: diff
changeset | 166 | |
| 
87e487ccbd7c
updated wordle testing
 Christian Urban <christian.urban@kcl.ac.uk> parents: diff
changeset | 167 | def frequencies(secrets: List[String]) : Map[Char, Double] = {
 | 
| 
87e487ccbd7c
updated wordle testing
 Christian Urban <christian.urban@kcl.ac.uk> parents: diff
changeset | 168 | val all = secrets.flatten | 
| 
87e487ccbd7c
updated wordle testing
 Christian Urban <christian.urban@kcl.ac.uk> parents: diff
changeset | 169 | all.groupBy(identity).view.mapValues(1.0D - _.size.toDouble / all.size ).toMap | 
| 
87e487ccbd7c
updated wordle testing
 Christian Urban <christian.urban@kcl.ac.uk> parents: diff
changeset | 170 | } | 
| 
87e487ccbd7c
updated wordle testing
 Christian Urban <christian.urban@kcl.ac.uk> parents: diff
changeset | 171 | |
| 
87e487ccbd7c
updated wordle testing
 Christian Urban <christian.urban@kcl.ac.uk> parents: diff
changeset | 172 | //frequencies(secrets)('y')
 | 
| 
87e487ccbd7c
updated wordle testing
 Christian Urban <christian.urban@kcl.ac.uk> parents: diff
changeset | 173 | |
| 
87e487ccbd7c
updated wordle testing
 Christian Urban <christian.urban@kcl.ac.uk> parents: diff
changeset | 174 | /* | 
| 
87e487ccbd7c
updated wordle testing
 Christian Urban <christian.urban@kcl.ac.uk> parents: diff
changeset | 175 | |
| 
87e487ccbd7c
updated wordle testing
 Christian Urban <christian.urban@kcl.ac.uk> parents: diff
changeset | 176 | def frequencies(secrets: List[String]) : Map[Char, Double] = {
 | 
| 
87e487ccbd7c
updated wordle testing
 Christian Urban <christian.urban@kcl.ac.uk> parents: diff
changeset | 177 | val letters = secrets.flatten | 
| 
87e487ccbd7c
updated wordle testing
 Christian Urban <christian.urban@kcl.ac.uk> parents: diff
changeset | 178 | val totalLetters = letters.length.toDouble | 
| 
87e487ccbd7c
updated wordle testing
 Christian Urban <christian.urban@kcl.ac.uk> parents: diff
changeset | 179 |     val alph = ('a' to 'z').toList
 | 
| 
87e487ccbd7c
updated wordle testing
 Christian Urban <christian.urban@kcl.ac.uk> parents: diff
changeset | 180 | val letterCount = alph.map(x => (x, letters.filter(_ == x).length.toDouble)) | 
| 
87e487ccbd7c
updated wordle testing
 Christian Urban <christian.urban@kcl.ac.uk> parents: diff
changeset | 181 | letterCount.map(x => (x._1, 1.0-(letterCount.filter(_._1 == x._1)(0)._2/totalLetters))).toMap | 
| 
87e487ccbd7c
updated wordle testing
 Christian Urban <christian.urban@kcl.ac.uk> parents: diff
changeset | 182 | } | 
| 
87e487ccbd7c
updated wordle testing
 Christian Urban <christian.urban@kcl.ac.uk> parents: diff
changeset | 183 | */ | 
| 
87e487ccbd7c
updated wordle testing
 Christian Urban <christian.urban@kcl.ac.uk> parents: diff
changeset | 184 | |
| 
87e487ccbd7c
updated wordle testing
 Christian Urban <christian.urban@kcl.ac.uk> parents: diff
changeset | 185 | // frequencies(secrets)('y')
 | 
| 
87e487ccbd7c
updated wordle testing
 Christian Urban <christian.urban@kcl.ac.uk> parents: diff
changeset | 186 | // frequencies(secrets)('e')
 | 
| 
87e487ccbd7c
updated wordle testing
 Christian Urban <christian.urban@kcl.ac.uk> parents: diff
changeset | 187 | |
| 
87e487ccbd7c
updated wordle testing
 Christian Urban <christian.urban@kcl.ac.uk> parents: diff
changeset | 188 | //============================= | 
| 
87e487ccbd7c
updated wordle testing
 Christian Urban <christian.urban@kcl.ac.uk> parents: diff
changeset | 189 | // Task 7 [1 Mark] | 
| 
87e487ccbd7c
updated wordle testing
 Christian Urban <christian.urban@kcl.ac.uk> parents: diff
changeset | 190 | |
| 
87e487ccbd7c
updated wordle testing
 Christian Urban <christian.urban@kcl.ac.uk> parents: diff
changeset | 191 | def rank(frqs: Map[Char, Double], s: String) = {
 | 
| 
87e487ccbd7c
updated wordle testing
 Christian Urban <christian.urban@kcl.ac.uk> parents: diff
changeset | 192 | s.map(frqs(_)).sum | 
| 
87e487ccbd7c
updated wordle testing
 Christian Urban <christian.urban@kcl.ac.uk> parents: diff
changeset | 193 | } | 
| 
87e487ccbd7c
updated wordle testing
 Christian Urban <christian.urban@kcl.ac.uk> parents: diff
changeset | 194 | |
| 
87e487ccbd7c
updated wordle testing
 Christian Urban <christian.urban@kcl.ac.uk> parents: diff
changeset | 195 | def ranked_evil(secrets: List[String], word: String) = {
 | 
| 
87e487ccbd7c
updated wordle testing
 Christian Urban <christian.urban@kcl.ac.uk> parents: diff
changeset | 196 | val frqs = frequencies(secrets) | 
| 
87e487ccbd7c
updated wordle testing
 Christian Urban <christian.urban@kcl.ac.uk> parents: diff
changeset | 197 | val ev = evil(secrets, word) | 
| 
87e487ccbd7c
updated wordle testing
 Christian Urban <christian.urban@kcl.ac.uk> parents: diff
changeset | 198 | ev.groupBy(rank(frqs, _)).toList.sortBy(_._1).reverse.head._2 | 
| 
87e487ccbd7c
updated wordle testing
 Christian Urban <christian.urban@kcl.ac.uk> parents: diff
changeset | 199 | } | 
| 
87e487ccbd7c
updated wordle testing
 Christian Urban <christian.urban@kcl.ac.uk> parents: diff
changeset | 200 | |
| 
87e487ccbd7c
updated wordle testing
 Christian Urban <christian.urban@kcl.ac.uk> parents: diff
changeset | 201 | /* | 
| 
87e487ccbd7c
updated wordle testing
 Christian Urban <christian.urban@kcl.ac.uk> parents: diff
changeset | 202 | def rank(frqs: Map[Char, Double], s: String) : Double = {
 | 
| 
87e487ccbd7c
updated wordle testing
 Christian Urban <christian.urban@kcl.ac.uk> parents: diff
changeset | 203 | val letters = s.toList | 
| 
87e487ccbd7c
updated wordle testing
 Christian Urban <christian.urban@kcl.ac.uk> parents: diff
changeset | 204 | val scores = letters.map(x => frqs(x).toDouble) | 
| 
87e487ccbd7c
updated wordle testing
 Christian Urban <christian.urban@kcl.ac.uk> parents: diff
changeset | 205 | scores.sum | 
| 
87e487ccbd7c
updated wordle testing
 Christian Urban <christian.urban@kcl.ac.uk> parents: diff
changeset | 206 | } | 
| 
87e487ccbd7c
updated wordle testing
 Christian Urban <christian.urban@kcl.ac.uk> parents: diff
changeset | 207 | |
| 
87e487ccbd7c
updated wordle testing
 Christian Urban <christian.urban@kcl.ac.uk> parents: diff
changeset | 208 | def ranked_evil(secrets: List[String], word: String) : List[String] = {
 | 
| 
87e487ccbd7c
updated wordle testing
 Christian Urban <christian.urban@kcl.ac.uk> parents: diff
changeset | 209 | val most = evil(secrets, word) | 
| 
87e487ccbd7c
updated wordle testing
 Christian Urban <christian.urban@kcl.ac.uk> parents: diff
changeset | 210 | val mapped = most.map(x => (x, rank(frequencies(secrets),x))) | 
| 
87e487ccbd7c
updated wordle testing
 Christian Urban <christian.urban@kcl.ac.uk> parents: diff
changeset | 211 | val mx = mapped.maxBy(_._2)._2 | 
| 
87e487ccbd7c
updated wordle testing
 Christian Urban <christian.urban@kcl.ac.uk> parents: diff
changeset | 212 | val filt = mapped.filter(_._2 == mx) | 
| 
87e487ccbd7c
updated wordle testing
 Christian Urban <christian.urban@kcl.ac.uk> parents: diff
changeset | 213 | filt.map(x => x._1) | 
| 
87e487ccbd7c
updated wordle testing
 Christian Urban <christian.urban@kcl.ac.uk> parents: diff
changeset | 214 | } | 
| 
87e487ccbd7c
updated wordle testing
 Christian Urban <christian.urban@kcl.ac.uk> parents: diff
changeset | 215 | */ | 
| 
87e487ccbd7c
updated wordle testing
 Christian Urban <christian.urban@kcl.ac.uk> parents: diff
changeset | 216 | //rank(frequencies(secrets), "adobe") | 
| 
87e487ccbd7c
updated wordle testing
 Christian Urban <christian.urban@kcl.ac.uk> parents: diff
changeset | 217 | //rank(frequencies(secrets), "gaffe") | 
| 
87e487ccbd7c
updated wordle testing
 Christian Urban <christian.urban@kcl.ac.uk> parents: diff
changeset | 218 | //rank(frequencies(secrets), "fuzzy") | 
| 
87e487ccbd7c
updated wordle testing
 Christian Urban <christian.urban@kcl.ac.uk> parents: diff
changeset | 219 | |
| 
87e487ccbd7c
updated wordle testing
 Christian Urban <christian.urban@kcl.ac.uk> parents: diff
changeset | 220 | //ranked_evil(secrets, "beats") | 
| 
87e487ccbd7c
updated wordle testing
 Christian Urban <christian.urban@kcl.ac.uk> parents: diff
changeset | 221 | //ranked_evil(secrets, "vitae") | 
| 
87e487ccbd7c
updated wordle testing
 Christian Urban <christian.urban@kcl.ac.uk> parents: diff
changeset | 222 | //ranked_evil(secrets, "bento") | 
| 
87e487ccbd7c
updated wordle testing
 Christian Urban <christian.urban@kcl.ac.uk> parents: diff
changeset | 223 | //ranked_evil(secrets, "belts") | 
| 
87e487ccbd7c
updated wordle testing
 Christian Urban <christian.urban@kcl.ac.uk> parents: diff
changeset | 224 | |
| 
87e487ccbd7c
updated wordle testing
 Christian Urban <christian.urban@kcl.ac.uk> parents: diff
changeset | 225 | } |