Skip to content

Instantly share code, notes, and snippets.

@tbje
Created December 16, 2011 09:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tbje/1485232 to your computer and use it in GitHub Desktop.
Save tbje/1485232 to your computer and use it in GitHub Desktop.
Test data
val mnemonics = Map(
'2' -> "ABC", '3' -> "DEF", '4' -> "GHI", '5' -> "JKL",
'6' -> "MNO", '7' -> "PQRS", '8' -> "TUV", '9' -> "WXYZ")
/**
* Inverse of the mnemonics map, e.g. 'A' -> '2', 'B' -> '2'.
*/
val charCode: Map[Char, Char] = for {
digitAndChars <- mnemonics
char <- digitAndChars._2
} yield char -> digitAndChars._1
/**
* Maps a word to its digit representation, e.g. "Java" -> "5282".
*/
def wordCode(word: String): String = word.toUpperCase map charCode
/**
* Groups all words by their number representations, e.g. "5282" -> Set("Java", "Kata", ...).
*/
val wordsForNumber: Map[String, Set[String]] = words groupBy wordCode
/**
* All ways to encode a number as a pharse, i.e. as a sequence of words.
*/
def encode(number: String): Set[Seq[String]] = {
if (number.isEmpty) Set(Seq()) else {
val splitPoints = (1 to number.length).toSet
for {
splitPoint <- splitPoints
prefix = number take (splitPoint)
postfix = number drop (splitPoint)
first <- wordsForNumber.getOrElse(prefix, Seq())
lastPart <- encode(postfix)
} yield first +: lastPart
}
}
/**
* Maps a number to the set of all phrases that can represent it.
*/
def translate(number: String): Set[String] = encode(number) map { _ mkString " " }
/*
* Copyright 2011 Typesafe Inc.
*
* This work is based on the original contribution of WeigleWilczek.
*
* Unless otherwise agreed, training materials may only be used for
* educational and reference purposes by individual named participants
* in a training course offered by Typesafe or a Typesafe training partner.
* Unauthorized reproduction, redistribution, or use of this material is prohibited.
*/
object TestData {
val (munich, nuremberg, frankfurt, cologne, essen) =
(Station("Munich"), Station("Nuremberg"), Station("Frankfurt"), Station("Cologne"), Station("Essen"))
val (munichTimeIce722, nurembergTimeIce722, frankfurtTimeIce722, essenTimeIce722) =
(Time(9, 55), Time(11), Time(13, 10), Time(15, 02))
val ice722 = Train("ICE", "722", Seq(
munichTimeIce722 -> munich,
nurembergTimeIce722 -> nuremberg,
frankfurtTimeIce722 -> frankfurt,
essenTimeIce722 -> essen))
val (munichTimeIce724, nurembergTimeIce724, frankfurtTimeIce724, cologneTimeIce724) =
(Time(8, 55), Time(10), Time(12, 10), Time(13, 39))
val ice724 = Train("ICE", "724", Seq(
munichTimeIce724 -> munich,
nurembergTimeIce724 -> nuremberg,
frankfurtTimeIce724 -> frankfurt,
cologneTimeIce724 -> cologne))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment