Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package week6
- import scala.io.Source
- object memonics {;import org.scalaide.worksheet.runtime.library.WorksheetSupport._; def main(args: Array[String])=$execute{;$skip(318);
- val in = Source.fromURL("http://lamp.epfl.ch/files/content/sites/lamp/files/teaching/progfun/linuxwords.txt");System.out.println("""in : scala.io.BufferedSource = """ + $show(in ));$skip(87);
- val words = in.getLines.toList filter (word => word forall (char => char.isLetter));System.out.println("""words : List[String] = """ + $show(words ));$skip(141);
- val nmem = Map(
- '2' -> "ABC", '3' -> "DEF", '4' -> "GHI", '5' -> "JKL",
- '6' -> "MNO", '7' -> "PQRS", '8' -> "TUV", '9' -> "WXYZ");System.out.println("""nmem : scala.collection.immutable.Map[Char,String] = """ + $show(nmem ));$skip(95);
- val charCode: Map[Char, Char] = for ((digit, str) <- nmem; ltr <- str) yield (ltr -> digit);System.out.println("""charCode : Map[Char,Char] = """ + $show(charCode ));$skip(70);
- def wordCode(word: String): String = word.toUpperCase map charCode;System.out.println("""wordCode: (word: String)String""");$skip(70);
- val wordsForNum: Map[String, Seq[String]] = words groupBy wordCode;System.out.println("""wordsForNum : Map[String,Seq[String]] = """ + $show(wordsForNum ));$skip(269);
- def encode(number: String): Set[List[String]] =
- if (number.isEmpty) Set(List())
- else{
- for {
- split <- 1 to number.length
- word <- wordsForNum(number take split)
- rest <- encode(number drop split)
- } yield word :: rest
- }.toSet;System.out.println("""encode: (number: String)Set[List[String]]""");$skip(81);
- def translate(number:String):Set[String]= encode(number) map (_ mkString " ");System.out.println("""translate: (number: String)Set[String]""");$skip(27); val res$0 =
- translate("7225247386");System.out.println("""res0: Set[String] = """ + $show(res$0))}
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement