How to resolve the algorithm Look-and-say sequence step by step in the Scala programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Look-and-say sequence step by step in the Scala programming language
Table of Contents
Problem Statement
The Look and say sequence is a recursively defined sequence of numbers studied most notably by John Conway.
The look-and-say sequence is also known as the Morris Number Sequence, after cryptographer Robert Morris, and the puzzle What is the next number in the sequence 1, 11, 21, 1211, 111221? is sometimes referred to as the Cuckoo's Egg, from a description of Morris in Clifford Stoll's book The Cuckoo's Egg.
Sequence Definition
An example:
Write a program to generate successive members of the look-and-say sequence.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Look-and-say sequence step by step in the Scala programming language
Source code in the scala programming language
import scala.annotation.tailrec
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)
}
}
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)
}
}
def lookAndSayIterator(seed: BigInt) = Iterator.iterate(seed)(lookAndSay)
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("")))
}
You may also check:How to resolve the algorithm Short-circuit evaluation step by step in the PowerShell programming language
You may also check:How to resolve the algorithm OpenGL step by step in the Pascal programming language
You may also check:How to resolve the algorithm Grayscale image step by step in the OCaml programming language
You may also check:How to resolve the algorithm Longest common substring step by step in the Delphi programming language
You may also check:How to resolve the algorithm Find common directory path step by step in the F# programming language