Sungchul Park avatar Sungchul Park committed cc7bd0f

개미, Iterater 사용하도록 변경

Comments (0)

Files changed (2)

src/scala/_homework/fupfin/lookandsay/LookAndSay.scala

 package _homework.fupfin.lookandsay
 
-object LookAndSay {
-  def getSequence(order:Int):List[Int] = {
-    require(order > 0)
-    calcSequence(List(1), order)
+class LookAndSay extends Iterator[List[Int]] {
+
+  private var lastElement:List[Int] = Nil
+
+  def hasNext = true
+  
+  def next:List[Int] = {
+    lastElement = countRunLength(lastElement, 0)
+    return lastElement
   }
 
-  private def calcSequence(current:List[Int], order:Int):List[Int] =
-    if(order > 1) calcSequence(countRunLength(current, 0), order -1 ) else current
-
   private def countRunLength(element:List[Int], count:Int):List[Int] = {
 
     element match {
-      case head :: tail => {}
+      case Nil => List(1)
+      case head :: tail =>
         if(tail == Nil)
-          List((count+1), head)
-        else {
-          if(head == tail(0))
-            countRunLength(tail, count+1)
-          else
-            List((count+1), head):::countRunLength(tail, 0)
-        }
+          List(count + 1, head)
+        else if(head == tail(0))
+          countRunLength(tail, count + 1)
+        else
+          List(count + 1, head):::countRunLength(tail, 0)
     }
-  }  
+  }
 }

test/scala/_homework/fupfin/lookandsay/LookAndSayTest.scala

 
 class LookAndSayTest {
   @Test
-  def testFirstElement() {
-    expect(List(1)) { LookAndSay.getSequence(1) }
-  }
-
-  @Test
-  def testCalc() {
-    var expects = List((2, List(1,1)), (3, List(2, 1)), (4, List(1, 2, 1, 1)))
+  def testIterator() {
+    val lookAndSay = new LookAndSay;
+    var expects = List(List(1), List(1, 1), List(2, 1), List(1, 2, 1, 1))
     expects.foreach( item => {
-      expect(item._2) { LookAndSay.getSequence(item._1)}
+      expect(item) {lookAndSay.next}
     })
   }
 }
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.