Skip to content

Instantly share code, notes, and snippets.

@bongster
Created July 11, 2014 13:12
Show Gist options
  • Save bongster/8a0e1cfd39fb5d8fd6da to your computer and use it in GitHub Desktop.
Save bongster/8a0e1cfd39fb5d8fd6da to your computer and use it in GitHub Desktop.
import java.io.{FileOutputStream, PrintWriter}
import scala.io.Source
/**
* Created by bongster on 2014. 7. 11..
*/
/*
* Single numbers just read them separately.
2 successive numbers use double.
3 successive numbers use triple.
4 successive numbers use quadruple.
5 successive numbers use quintuple.
6 successive numbers use sextuple.
7 successive numbers use septuple.
8 successive numbers use octuple.
9 successive numbers use nonuple.
10 successive numbers use decuple.
More than 10 successive numbers read them all separately.
*
* */
/*
*
*
* */
case class PhoneNumber(num:String , seq: List[Int]){
val CountToString = Map(1 -> "",2 -> "double", 3-> "triple", 4-> "quadruple"
,5-> "quintuple", 6-> "sextuple" ,7 -> "septuple" ,8 -> "octuple" ,9 -> "nonuple" , 10 -> "decuple" )
val NumtoString = Map("1" -> "one","2" -> "two", "3"-> "three", "4"-> "four"
,"5"-> "five", "6"-> "six" ,"7" -> "seven" ,"8" -> "eight" ,"9" -> "nine" ,"0" -> "zero" )
val numList = getNumList(num,seq)
def getNumList(num:String , sep: List[Int]): List[String] =
if (sep.isEmpty) Nil
else
num.take(sep.head) :: getNumList(num.drop(sep.head), sep.tail)
def getGroupList(n : String): List[(String,Int)] =
if(n.isEmpty) Nil
else {
val (h, t) = n.span(p => p == n.head)
(n.head.toString -> h.size) :: getGroupList(t)
}
def solve : String =(for{
num <- numList
(n,c) <- getGroupList(num)
//nn = println(s"($n, $c)")
} yield (n,c) match {
case (_, 1) => NumtoString(n)
case _ if c > 10 => println(s"tttt$c, $n") ;(1 to c).map(x => NumtoString(n) ).mkString(" ")
case _ => CountToString(c) + " " + NumtoString(n)
}).mkString(" ")
}
object Main {
def main(args: Array[String]) {
val writer = new PrintWriter(new FileOutputStream("sample-large.out"),true);
val lines = Source.fromFile("/Users/bongster/IdeaProjects/RaScala/ReadNumber/A-large-practice.in").getLines()
val caseNum = lines.next().toInt
(1 to caseNum) foreach { cn=>
val Array(num,sep) = lines.next().split(" ")
val sepList: List[Int] = sep.split("-").map(_.toInt).toList
val result = PhoneNumber(num,sepList).solve
writer.println(s"Case #$cn: ${result}")
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment