diff -r b528d1d3d3c3 -r 59e005dcf163 main_testing2/wordle.scala --- a/main_testing2/wordle.scala Thu Nov 02 13:53:37 2023 +0000 +++ b/main_testing2/wordle.scala Thu Nov 02 23:34:53 2023 +0000 @@ -9,45 +9,25 @@ // ADD YOUR CODE BELOW //====================== -// def main(args: Array[String]): Unit = { -// val secrets = get_wordle_list("https://nms.kcl.ac.uk/christian.urban/wordle.txt") -// println(ranked_evil(secrets, "beats")== List("fuzzy")) -// println(ranked_evil(secrets, "vitae") == List("fuzzy")) -// println(ranked_evil(secrets, "bento") == List("fuzzy")) -// println(ranked_evil(secrets, "belts") == List("fuzzy")) -// println(ranked_evil(secrets, "abbey") == List("whizz")) -// println(ranked_evil(secrets, "afear") == List("buzzy")) -// println(ranked_evil(secrets, "zincy") == List("jugum")) -// println(ranked_evil(secrets, "zippy") == List("chuff")) - - -// } //(1) def get_wordle_list(url: String) : List[String] = { - try { - Source.fromURL(url).getLines.toList; - } - catch { - case _ : Throwable => List(); - } + Try(Source.fromURL(url).getLines.toList).getOrElse(List()) } + // val secrets = get_wordle_list("https://nms.kcl.ac.uk/christian.urban/wordle.txt") // secrets.length // => 12972 // secrets.filter(_.length != 5) // => Nil -//(2) -def removeN[A](xs: List[A], elem: A, n: Int) : List[A] = { - if (xs.isEmpty || n == 0 || !xs.contains(elem)){ - xs - } - else{ - val (newLst,newLst2) = xs.splitAt(xs.indexOf(elem)) - removeN(newLst ++ newLst2.tail,elem, n-1) - } +//(2) +def removeN[A](xs: List[A], elem: A, n: Int) : List[A] = xs match { + case Nil => Nil + case x :: tail if x == elem && n > 0 => removeN(tail, elem, n - 1) + case x :: tail => x :: removeN(tail, elem, n) } + // removeN(List(1,2,3,2,1), 3, 1) // => List(1, 2, 2, 1) // removeN(List(1,2,3,2,1), 2, 1) // => List(1, 3, 2, 1) // removeN(List(1,2,3,2,1), 1, 1) // => List(2, 3, 2, 1) @@ -61,42 +41,19 @@ def pool(secret: String, word: String) : List[Char] = { - //print(secret.toList, word.toList) - val lst= ((0 to 4).map(x => { - if(!secret(x).equals(word(x))) Some(secret(x)) else None - - }).toList) - lst.flatten + (for(index <- (0 to word.length-1); if (word(index) != secret(index))) yield secret(index).toChar).toList } - - +// t def aux(secret: List[Char], word: List[Char], pool: List[Char]) : List[Tip] = { - - if (secret.length == 1){ - if (secret.head == word.head){ - List(Correct) - } - else if (pool.contains(word.head)){ - List(Present) - } - else { - List(Absent) - } - } - else if (secret.head == word.head){ - List(Correct) ++ aux(secret.tail, word.tail, pool) - } - else if (pool.contains(word.head)){ - List(Present) ++ aux(secret.tail, word.tail, removeN(pool, word.head, 1)) - } + if (secret.isEmpty) List() else { - List(Absent) ++ aux(secret.tail, word.tail, pool) + if (secret.head == word.head) Correct :: aux(secret.tail, word.tail, pool) + else if (pool.contains(word.head)) Present :: aux(secret.tail, word.tail, pool.filterNot(_ == word.head)) + else Absent :: aux(secret.tail, word.tail, pool) } } -def score(secret: String, word: String) : List[Tip] = { - aux(secret.toList,word.toList,pool(secret,word)) -} +def score(secret: String, word: String) : List[Tip] = aux(secret.toList, word.toList, pool(secret, word)) // score("chess", "caves") // => List(Correct, Absent, Absent, Present, Correct) @@ -105,38 +62,31 @@ // score("chess", "eexss") // => List(Present, Absent, Absent, Correct, Correct) // (4) -def eval(t: Tip) : Int = { - if (t == Correct) 10 - else if (t == Present) 1 - else 0 +def eval(t: Tip) : Int = t match { + case Correct => 10 + case Present => 1 + case Absent => 0 } -def iscore(secret: String, word: String) : Int = { - val scoreList = score(secret,word) - scoreList.map(x =>(eval(x))).toList.sum -} +def iscore(secret: String, word: String) : Int = (for(current <- score(secret, word)) yield eval(current)).sum + //iscore("chess", "caves") // => 21 //iscore("chess", "swiss") // => 20 -// (5) +// (5) t def lowest(secrets: List[String], word: String, current: Int, acc: List[String]) : List[String] = { - if(secrets.isEmpty) acc - else if (iscore(secrets.head,word) (x._1, (1-(x._2.size.toDouble/ totalChar)))) - + val occurrences = secrets.mkString.groupBy(identity).mapValues(_.length) + val all_letters = secrets.mkString.length.toDouble + occurrences.map{ case (c, n) => c -> (1 - n/all_letters) }.toMap } -// (7) -def rank(frqs: Map[Char, Double], s: String) = { - s.map(x => (frqs(x))).toList.sum -} - -def ranked_evil(secrets: List[String], word: String) : List[String]= { - val evilWords = evil(secrets,word) - val returnVal = evilWords.map(x => (x, rank(frequencies(secrets),x))).toMap.maxBy(_._2)._1 - List(returnVal) -} +// (7) +def rank(frqs: Map[Char, Double], s: String) : Double = s.map(c => frqs(c)).sum +def ranked_evil(secrets: List[String], word: String): List[String] = { + val frqs = frequencies(secrets) + val ranked_secrets = evil(secrets, word).map(s => (s, rank(frqs, s))) + List((ranked_secrets.sortWith(_._2 > _._2)).map(_._1).head ) +} + }