k47.cz
výběr kolo foto makro povídky kultura
koronavirus TECH ▞▞ 🞄⬤🞄 | twitter RSS

Scrabble

— k47 (CC by-sa)

Skript na­psaný ve Scale, který se se­znamu slov vybere ty, za něž by ve Scra­bble bylo nej­více bodů.

val letters = Map(
'a' -> 1, 'é' -> 3, 'k' -> 1, 'r' -> 1, 'ů' -> 4,
'á' -> 2, 'ě' -> 3, 'l' -> 1, 'ř' -> 4, 'v' -> 1,
'b' -> 3, 'f' -> 5, 'm' -> 2, 's' -> 1, 'x' -> 10,
'c' -> 2, 'g' -> 5, 'n' -> 1, 'š' -> 4, 'y' -> 2,
'č' -> 4, 'h' -> 2, 'ň' -> 6, 't' -> 1, 'ý' -> 4,
'd' -> 1, 'i' -> 1, 'o' -> 1, 'ť' -> 7, 'z' -> 2,
'ď' -> 8, 'í' -> 2, 'ó' -> 7, 'u' -> 2, 'ž' -> 4,
'e' -> 1, 'j' -> 2, 'p' -> 1, 'ú' -> 5).withDefaultValue(0)

val words = io.Source.fromFile("seznam_slov").getLines.toSeq
val wordsScore = words.map { word => (word, word.map(letters).sum) }
val ordered = wordsScore.sortBy { case (word, score) => score }.reverse
ordered.take(10).foreach(println)

Al­ter­na­tivní forma:

val letters = Map(
  1 -> "adeiklnoprstv",
  2 -> "jyumáíchz",
  3 -> "béě",
  4 -> "čýšžřů",
  5 -> "fgú",
  6 -> "ň",
  7 -> "ťó",
  8 -> "ď",
  10 -> "x"
).flatMap { case (k, vs) => vs.map(_ -> k) }.withDefaultValue(0)

...
píše k47 & hosté, ascii@k47.cz