1. ms shin
  2. ScalaStudy

Commits

ms shin  committed 37bb833

당신이 잠든사이에 몰래 변경되는 개미수열 풀이.

  • Participants
  • Parent commits 2428344
  • Branches default

Comments (0)

Files changed (1)

File src/scala/_homework/anarcher/antseq/antseq.scala

View file
-import collection.mutable.ListBuffer
-
-def e(xs : List[String],x : StringBuffer = new StringBuffer()) : String = xs match {
-    case h :: _ => {
-        x.append("%s%s".format(xs.takeWhile( _ == h ) size , h ))
-        val ys = xs.dropWhile( _ == h )
-        e(ys,x)
-    }
-    case Nil => x.toString
-}
-def antseq(num:Int=7,s:String="1") : List[String] = {
-    val r = new ListBuffer[String]
-    r.append(s)
-    var a = s
-    for(i <- 1 to num) {
-        a = e(a.toList.map( _.toString ))
-        r.append(a)
-    }
-    r.toList
+def seq(xs: List[Int],ys:List[Int] = List[Int]() ) : List[Int] = xs match {
+  case head :: _ => {
+    val cnt = xs.takeWhile( _ == head).size
+    val tail = xs.drop(cnt)
+    seq(tail,ys ::: cnt :: head :: Nil)
+  }
+  case _ => ys 
 }
 
-antseq().mkString("\n")
+var s = List(1)
+var seqs = s :: Nil
+
+for(i <- 1 to 7) {
+  s = seq(s)  
+  seqs = s :: seqs
+}
+
+for(s <- seqs.reverse) {
+  print("%s\n".format(s.mkString("")))
+}
+
+
+// vim: set ts=4 sw=4 et: