Skip to content

Instantly share code, notes, and snippets.

@SegFaultAX
Created October 25, 2013 04:44
Show Gist options
  • Save SegFaultAX/7149532 to your computer and use it in GitHub Desktop.
Save SegFaultAX/7149532 to your computer and use it in GitHub Desktop.
package forcomp
import common._
object Phonenumber {
val dictionary = loadDictionary
val keyMap = Map(
'2' -> "ABC", '3' -> "DEF", '4' -> "GHI", '5' -> "JKL",
'6' -> "MNO", '7' -> "PQRS", '8' -> "TUV", '9' -> "WXYZ"
)
val letterMap = for ((num, letters) <- keyMap; letter <- letters) yield (letter -> num)
def wordToNumber(word: String): String =
word.toUpperCase.map(letterMap)
lazy val keypadDictionary =
dictionary
.filter(w => w.forall(_.isLetter))
.groupBy(wordToNumber)
.withDefaultValue(List())
def anagrams(number: String): List[List[String]] =
if (number.isEmpty) List(List())
else
(for {
n <- (1 to number.length)
word <- keypadDictionary(number.take(n))
rest <- anagrams(number.drop(n))
} yield word :: rest).toList
def main(args: Array[String]) =
anagrams("7225247386") map (_.mkString(" ")) foreach println
//Output:
//> pack ah re to
//| pack air fun
//| pack bird to
//| rack ah re to
//| rack air fun
//| rack bird to
//| sack ah re to
//| sack air fun
//| sack bird to
//| Scala is fun
//| Scala ire to
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment