updated jars
authorChristian Urban <christian.urban@kcl.ac.uk>
Wed, 09 Nov 2022 20:45:57 +0000
changeset 439 97594b9998a8
parent 438 811cf79546da
child 440 f3bfc496d6ca
updated jars
core_solution1/collatz.jar
core_solution2/docdiff.jar
core_solution3/postfix.jar
core_solution3/postfix2.jar
core_templates1/collatz.jar
core_templates2/docdiff.jar
core_templates3/postfix.jar
core_templates3/postfix2.jar
main_solution1/drumb.jar
main_solution3/re.jar
main_templates1/drumb.jar
main_templates2/wordle.jar
main_templates3/re.jar
main_templates4/knight1.jar
main_templates4/knight2.jar
main_templates4/knight3.jar
main_templates4/knight4.jar
main_templates5/bf.jar
main_templates5/bfc.jar
main_testing2/wordle.scala
main_testing2/wordle_test.sh
main_testing2/wordle_test2.scala
Binary file core_solution1/collatz.jar has changed
Binary file core_solution2/docdiff.jar has changed
Binary file core_solution3/postfix.jar has changed
Binary file core_solution3/postfix2.jar has changed
Binary file core_templates1/collatz.jar has changed
Binary file core_templates2/docdiff.jar has changed
Binary file core_templates3/postfix.jar has changed
Binary file core_templates3/postfix2.jar has changed
Binary file main_solution1/drumb.jar has changed
Binary file main_solution3/re.jar has changed
Binary file main_templates1/drumb.jar has changed
Binary file main_templates2/wordle.jar has changed
Binary file main_templates3/re.jar has changed
Binary file main_templates4/knight1.jar has changed
Binary file main_templates4/knight2.jar has changed
Binary file main_templates4/knight3.jar has changed
Binary file main_templates4/knight4.jar has changed
Binary file main_templates5/bf.jar has changed
Binary file main_templates5/bfc.jar has changed
--- a/main_testing2/wordle.scala	Wed Nov 09 18:44:40 2022 +0000
+++ b/main_testing2/wordle.scala	Wed Nov 09 20:45:57 2022 +0000
@@ -1,63 +1,54 @@
-// Resit Part about an Evil Wordle Clone
-//==========================================
-//
-// Task description are on KEATS
-//
-// Make sure you use Scala Version 2.13 (not Scala 3)
-//
-// Upload your submision to OneSpace and give Senir and Christian
-// write access to the folder. The folder should have you email
-// address as name. Give us write permission by July 25th such
-// that we can download your solution by the closing date August 4th.
+// Main Part 3 about Evil Wordle
+//===============================
 
-object Resit {
+// test bash
+
+object M2 { 
 
 import io.Source
 import scala.util._
 
-//=============================
-// Task 1 [0.25 Marks]
+// ADD YOUR CODE BELOW
+//======================
 
+// test import
+
+
+//(1)
 def get_wordle_list(url: String) : List[String] = {
-    try{
-		val raw = Source.fromURL(url).mkString
-		raw.split("\n").toList
-	}
-	catch {
-		case e : Exception => List[String]()
-    }
+  Try(Source.fromURL(url)("ISO-8859-1").getLines().toList).getOrElse(Nil)
 }
 
 
 // val secrets = get_wordle_list("https://nms.kcl.ac.uk/christian.urban/wordle.txt")
-// secrets.length => 12972
-// secrets.filter(_.length != 5) => Nil
-
-//=============================
-// Task 2 [0.25 Marks]
+// secrets.length // => 12972
+// secrets.filter(_.length != 5) // => Nil
 
-def removeOne[A](xs: List[A], elem: A) : List[A] = {
-    val index = xs.indexOf(elem)
-	val first = xs.take(index)
-	val second = xs.drop(index + 1)
-	first ::: second
-}
-
-// removeOne(List(1,2,3,2,1), 3)  => List(1, 2, 2, 1)
-// removeOne(List(1,2,3,2,1), 2)  => List(1, 3, 2, 1)
-// removeOne(List(1,2,3,2,1), 1)  => List(2, 3, 2, 1)
-// removeOne(List(1,2,3,2,1), 0)  => List(1, 2, 3, 2, 1)
+//(2)
+def removeN[A](xs: List[A], elem: A, n: Int) : List[A] = {
+    if (n == 0) xs 
+    else xs match {
+        case Nil => Nil
+        case x::xs => 
+            if (x == elem) removeN(xs, x, n - 1) 
+            else x::removeN(xs, elem, n)
+    } 
+}    
 
 
-//=============================
-// Task 3 [1.5 Marks]
+// 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)
+// removeN(List(1,2,3,2,1), 0, 2)  // => List(1, 2, 3, 2, 1)
 
+// (3)
 abstract class Tip
 case object Absent extends Tip
 case object Present extends Tip
 case object Correct extends Tip
 
-def pool(secret: String, word: String) : List[Char]= {
+
+def pool(secret: String, word: String) : List[Char] = {
   for (i <- (0 to 4).toList 
        if secret(i) != word(i))
   yield secret(i) 
@@ -67,159 +58,83 @@
     case (Nil, Nil) => Nil
     case (s::srest, w::wrest) => {
         if (s == w) Correct::aux(srest, wrest, pool)
-        else if (pool.contains(w)) Present::aux(srest, wrest, removeOne(pool, w))
+        else if (pool.contains(w)) Present::aux(srest, wrest, removeN(pool, w, 1))
         else  Absent::aux(srest, wrest, pool)
     }
 }
 
-def score(secret: String, word: String) : List[Tip] =
-    aux(secret.toList, word.toList, pool(secret, word))
 
-/*
-def pool(secret: String, word: String) : List[Char] = {
-	(secret zip word).collect {
-		case (c1, c2) if c1 != c2 => s"$c1"
-	}.toList.flatten
-}
-
-def aux2(secret: String, word: String, pool: List[Char], result: List[Tip]) : List[Tip] = {
-    if (word.length == 0){
-        result.reverse
-    }
-    else{
-		if (word.take(1) == secret.take(1)) {
-			val updated = Correct :: result	
-			aux2(secret.drop(1), word.drop(1), pool, updated)
-		}	
-		else {
-			if(pool.contains(word.take(1)(0))){
-				val updated = Present :: result
-				val removed = removeOne(pool, word.take(1)(0)).mkString.toList 
-				aux2(secret.drop(1), word.drop(1), removed, updated)
-			}
-			else{
-				val updated = Absent :: result
-				aux2(secret.drop(1), word.drop(1), pool, updated)
-			}
-		}
-	}
-}
-
-def aux(secret: List[Char], word: List[Char], pool: List[Char]) : List[Tip] = {
-    aux2(secret.mkString, word.mkString, pool, List())
-}
-
-def score(secret: String, word: String) : List[Tip] = {
-    aux(secret.toList, word.toList, pool(secret, word))
-}
-*/
-
-// score("chess", "caves")
-// score("doses", "slide")
-// score("chess", "swiss")
-// score("chess", "eexss")
+def score(secret: String, word: String) : List[Tip] = 
+  aux(secret.toList, word.toList, pool(secret, word))
 
 
-//=============================
-// Task 4 [0.5 Marks]
+// score("chess", "caves") // => List(Correct, Absent, Absent, Present, Correct)
+// score("doses", "slide") // => List(Present, Absent, Absent, Present, Present)
+// score("chess", "swiss") // => List(Absent, Absent, Absent, Correct, Correct)
+// score("chess", "eexss") // => List(Present, Absent, Absent, Correct, Correct)
 
+// (4)
 def eval(t: Tip) : Int = t match {
     case Correct => 10
     case Present => 1
     case Absent => 0
 }
 
-def iscore(secret: String, word: String) : Int = {
-    val list = score(secret, word)
-	val values = list.map(n => eval(n))
-	values.sum
+def iscore(secret: String, word: String) : Int = 
+  score(secret, word).map(eval).sum
+
+//iscore("chess", "caves") // => 21
+//iscore("chess", "swiss") // => 20
+
+// (5)
+def lowest(secrets: List[String], word: String, current: Int, acc: List[String]) : List[String] = secrets match {
+    case Nil => acc
+    case s::srest => {
+        val nscore = iscore(s, word)
+        if (nscore < current) lowest(srest, word, nscore, List(s)) 
+        else if (nscore == current) lowest(srest, word, current, s::acc)
+        else  lowest(srest, word, current, acc)
+    }
 }
 
-// iscore("chess", "caves")
-// iscore("doses", "slide")
-// iscore("chess", "swiss")
-// iscore("chess", "eexss")
-
-//=============================
-// Task 5 [1.5 Mark]
-
-def lowest(secrets: List[String], word: String, current: Int, acc: List[String]) : List[String] = {
-    val mapped = secrets.map(x => (x, iscore(x, word)))
-    val min = mapped.minBy(_._2)._2
-    val filt = mapped.filter(_._2 == min)
-    filt.map(x => x._1)
-}
-
-def evil(secrets: List[String], word: String) : List[String] = {
-    lowest(secrets, word, Int.MaxValue, List())
-}
-
-//evil(secrets, "stent").length 
-//evil(secrets, "hexes").length 
-//evil(secrets, "horse").length 
-//evil(secrets, "hoise").length 
-//evil(secrets, "house").length 
+def evil(secrets: List[String], word: String) = 
+  lowest(secrets, word, Int.MaxValue, Nil)
 
 
-//=============================
-// Task 6 [1 Mark]
+//evil(secrets, "stent").length
+//evil(secrets, "hexes").length
+//evil(secrets, "horse").length
+//evil(secrets, "hoise").length
+//evil(secrets, "house").length
 
+// (6)
 def frequencies(secrets: List[String]) : Map[Char, Double] = {
     val all = secrets.flatten
     all.groupBy(identity).view.mapValues(1.0D - _.size.toDouble / all.size ).toMap
 }
 
-//frequencies(secrets)('y')
-
-/*
-
-def frequencies(secrets: List[String]) : Map[Char, Double] = {
-    val letters = secrets.flatten
-    val totalLetters = letters.length.toDouble
-    val alph = ('a' to 'z').toList
-    val letterCount = alph.map(x => (x, letters.filter(_ == x).length.toDouble))
-    letterCount.map(x => (x._1, 1.0-(letterCount.filter(_._1 == x._1)(0)._2/totalLetters))).toMap
-}
-*/
-
-// frequencies(secrets)('y')
-// frequencies(secrets)('e')
-
-//=============================
-// Task 7 [1 Mark]
-
+// (7)
 def rank(frqs: Map[Char, Double], s: String) = {
     s.map(frqs(_)).sum
 }
 
+
 def ranked_evil(secrets: List[String], word: String) = {
     val frqs = frequencies(secrets)
     val ev = evil(secrets, word)
     ev.groupBy(rank(frqs, _)).toList.sortBy(_._1).reverse.head._2
 }
 
-/*
-def rank(frqs: Map[Char, Double], s: String) : Double = {
-    val letters = s.toList
-    val scores = letters.map(x => frqs(x).toDouble)
-    scores.sum
+
 }
 
-def ranked_evil(secrets: List[String], word: String) : List[String] = {
-    val most = evil(secrets, word)
-    val mapped = most.map(x => (x, rank(frequencies(secrets),x)))
-    val mx = mapped.maxBy(_._2)._2
-    val filt = mapped.filter(_._2 == mx)
-    filt.map(x => x._1)
-}
-*/
-//rank(frequencies(secrets), "adobe") 
-//rank(frequencies(secrets), "gaffe") 
-//rank(frequencies(secrets), "fuzzy") 
+
+
+
+
 
-//ranked_evil(secrets, "beats") 
-//ranked_evil(secrets, "vitae") 
-//ranked_evil(secrets, "bento") 
-//ranked_evil(secrets, "belts") 
-
-}
\ No newline at end of file
+// This template code is subject to copyright 
+// by King's College London, 2022. Do not 
+// make the template code public in any shape 
+// or form, and do not exchange it with other 
+// students under any circumstance.
--- a/main_testing2/wordle_test.sh	Wed Nov 09 18:44:40 2022 +0000
+++ b/main_testing2/wordle_test.sh	Wed Nov 09 20:45:57 2022 +0000
@@ -92,11 +92,13 @@
 if [ $tsts -eq 0 ]
 then
   echo "Task 2:"  >> $out  
-  echo "  removeOne(List(1,2,3,2,1), 3) == List(1, 2, 2, 1)" >> $out
-  echo "  removeOne(List(1,2,3,2,1), 2) == List(1, 3, 2, 1)" >> $out
-  echo "  removeOne(List(1,2,3,2,1), 1) == List(2, 3, 2, 1)" >> $out
-  echo "  removeOne(List(1,2,3,2,1), 0) == List(1, 2, 3, 2, 1)" >> $out
-  echo "  removeOne(List(\"a\", \"b\", \"b\"), \"b\") == List(\"a\", \"b\")" >> $out
+  echo "  removeN(List(1,2,3,2,1), 3, 1) == List(1, 2, 2, 1)" >> $out
+  echo "  removeN(List(1,2,3,2,1), 2, 1) == List(1, 3, 2, 1)" >> $out
+  echo "  removeN(List(1,2,3,2,1), 1, 1) == List(2, 3, 2, 1)" >> $out
+  echo "  removeN(List(1,2,3,2,1), 0, 1) == List(1, 2, 3, 2, 1)" >> $out
+  echo "  removeN(List(\"a\", \"b\", \"b\"), \"b\", 1) == List(\"a\", \"b\")" >> $out
+  echo "  removeN(List(\"b\", \"a\", \"b\", \"b\"), \"b\", 2) == List(\"a\", \"b\")" >> $out
+  echo "  removeN(List(\"b\", \"a\", \"b\"), \"a\", 2) == List(\"b\", \"b\")" >> $out
 
   if (scala_assert "wordle.scala" "wordle_test2.scala")
   then
--- a/main_testing2/wordle_test2.scala	Wed Nov 09 18:44:40 2022 +0000
+++ b/main_testing2/wordle_test2.scala	Wed Nov 09 20:45:57 2022 +0000
@@ -2,8 +2,16 @@
 import M2._
 
 
-assert(removeOne(List(1,2,3,2,1), 3) == List(1, 2, 2, 1))
-assert(removeOne(List(1,2,3,2,1), 2) == List(1, 3, 2, 1))
-assert(removeOne(List(1,2,3,2,1), 1) == List(2, 3, 2, 1))
-assert(removeOne(List(1,2,3,2,1), 0) == List(1, 2, 3, 2, 1))
-assert(removeOne(List("a", "b", "b"), "b") == List("a", "b"))
+assert(removeN(List(1,2,3,2,1), 3, 1)  == List(1, 2, 2, 1))
+assert(removeN(List(1,2,3,2,1), 2, 1)  == List(1, 3, 2, 1))
+assert(removeN(List(1,2,3,2,1), 1, 1)  == List(2, 3, 2, 1))
+assert(removeN(List(1,2,3,2,1), 0, 2)  == List(1, 2, 3, 2, 1))
+assert(removeN(List("a", "b", "b"), "b", 1) == List("a", "b"))
+assert(removeN(List("b", "a", "b", "b"), "b", 2) == List("a", "b"))
+assert(removeN(List("b", "a", "b"), "a", 2) == List("b", "b"))
+
+//assert(removeOne(List(1,2,3,2,1), 3) == List(1, 2, 2, 1))
+//assert(removeOne(List(1,2,3,2,1), 2) == List(1, 3, 2, 1))
+//assert(removeOne(List(1,2,3,2,1), 1) == List(2, 3, 2, 1))
+//assert(removeOne(List(1,2,3,2,1), 0) == List(1, 2, 3, 2, 1))
+