June 2018 Update
This commit is contained in:
parent
ba8067c3b7
commit
22f33d4004
5278 changed files with 84726 additions and 14379 deletions
|
|
@ -1,11 +1,27 @@
|
|||
def lookAndSay(seed: BigInt) = {
|
||||
val s = seed.toString
|
||||
( 1 until s.size).foldLeft((1, s(0), new StringBuilder)) {
|
||||
case ((len, c, sb), index) if c != s(index) => sb.append(len); sb.append(c); (1, s(index), sb)
|
||||
case ((len, c, sb), _) => (len + 1, c, sb)
|
||||
} match {
|
||||
case (len, c, sb) => sb.append(len); sb.append(c); BigInt(sb.toString)
|
||||
}
|
||||
}
|
||||
import scala.annotation.tailrec
|
||||
|
||||
def lookAndSayIterator(seed: BigInt) = Iterator.iterate(seed)(lookAndSay)
|
||||
object LookAndSay extends App {
|
||||
|
||||
loop(10, "1")
|
||||
|
||||
@tailrec
|
||||
private def loop(n: Int, num: String): Unit = {
|
||||
println(num)
|
||||
if (n <= 0) () else loop(n - 1, lookandsay(num))
|
||||
}
|
||||
|
||||
private def lookandsay(number: String): String = {
|
||||
val result = new StringBuilder
|
||||
|
||||
@tailrec
|
||||
def loop(numberString: String, repeat: Char, times: Int): String =
|
||||
if (numberString.isEmpty) result.toString()
|
||||
else if (numberString.head != repeat) {
|
||||
result.append(times).append(repeat)
|
||||
loop(numberString.tail, numberString.head, 1)
|
||||
} else loop(numberString.tail, numberString.head, times + 1)
|
||||
|
||||
loop(number.tail + " ", number.head, 1)
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,17 +1,11 @@
|
|||
object Main extends App {
|
||||
|
||||
def lookAndSay(previous: List[BigInt]): Stream[List[BigInt]] = {
|
||||
|
||||
def next(num: List[BigInt]): List[BigInt] = num match {
|
||||
case Nil => Nil
|
||||
case head :: Nil => 1 :: head :: Nil
|
||||
case head :: tail =>
|
||||
val size = (num takeWhile (_ == head)).size
|
||||
List(BigInt(size), head) ::: next(num.drop(size))
|
||||
}
|
||||
val x = next(previous)
|
||||
x #:: lookAndSay(x)
|
||||
def lookAndSay(seed: BigInt) = {
|
||||
val s = seed.toString
|
||||
( 1 until s.size).foldLeft((1, s(0), new StringBuilder)) {
|
||||
case ((len, c, sb), index) if c != s(index) => sb.append(len); sb.append(c); (1, s(index), sb)
|
||||
case ((len, c, sb), _) => (len + 1, c, sb)
|
||||
} match {
|
||||
case (len, c, sb) => sb.append(len); sb.append(c); BigInt(sb.toString)
|
||||
}
|
||||
|
||||
(lookAndSay(1 :: Nil) take 10).foreach(s => println(s.mkString("")))
|
||||
}
|
||||
|
||||
def lookAndSayIterator(seed: BigInt) = Iterator.iterate(seed)(lookAndSay)
|
||||
|
|
|
|||
|
|
@ -0,0 +1,17 @@
|
|||
object Main extends App {
|
||||
|
||||
def lookAndSay(previous: List[BigInt]): Stream[List[BigInt]] = {
|
||||
|
||||
def next(num: List[BigInt]): List[BigInt] = num match {
|
||||
case Nil => Nil
|
||||
case head :: Nil => 1 :: head :: Nil
|
||||
case head :: tail =>
|
||||
val size = (num takeWhile (_ == head)).size
|
||||
List(BigInt(size), head) ::: next(num.drop(size))
|
||||
}
|
||||
val x = next(previous)
|
||||
x #:: lookAndSay(x)
|
||||
}
|
||||
|
||||
(lookAndSay(1 :: Nil) take 10).foreach(s => println(s.mkString("")))
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue