Guest User

Untitled

a guest
Oct 19th, 2017
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scala 1.38 KB | None | 0 0
  1. package demo
  2.  
  3. class Coder(words: List[String]) {
  4.  
  5.   private val mnemonics = Map(
  6.       '2' -> "ABC", '3' -> "DEF", '4' -> "GHI", '5' -> "JKL",
  7.       '6' -> "MNO", '7' -> "PQRS", '8' -> "TUV", '9' -> "WXYZ")
  8.      
  9.   /** Invert the mnemonics map to give a map from chars 'A' ... 'Z' to '2' ... '9' */
  10.   private val charCode: Map[Char, Char] = ???
  11.          
  12.   /** Maps a word to the digit string it can represent, e.g. “Java” -> “5282” */
  13.   private def wordCode(word: String): String = ???
  14.    
  15.   /** A map from digit strings to the words that represent them,
  16.     * e,g. “5282” -> List(“Java”, “Kata”, “Lava”, ...)
  17.     * Note: A missing number should map to the empty set, e.g. "1111" -> List()
  18.     */
  19.   private val wordsForNum: Map[String, Seq[String]] = ???
  20.  
  21.   /** Return all ways to encode a number as a list of words */
  22.   def encode(number: String): Set[List[String]] = ???
  23.    
  24.   /** Maps a number to a list of all word phrases that can represent it */
  25.   def translate(number: String): Set[String] = encode(number) map (_ mkString " ")
  26.  
  27.   def ??? = throw new Error("not implemented")
  28. }
  29.  
  30. object Coder extends App {
  31.   val dict = io.Source.fromFile("/usr/share/dict/words")
  32.      .getLines.filter(_.length > 1).filter(_.matches("[a-zA-Z]+")).toList  
  33.   val coder = new Coder("Scala" :: "rocks" :: dict)
  34.   println(coder.translate("7225276257"))
  35. }
Add Comment
Please, Sign In to add comment