Skip to content

Instantly share code, notes, and snippets.

@sinanduman
Created February 14, 2017 04:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sinanduman/b8df31b80b290e460ff8ab0c26256884 to your computer and use it in GitHub Desktop.
Save sinanduman/b8df31b80b290e460ff8ab0c26256884 to your computer and use it in GitHub Desktop.
Scala solution for the algorithm explained in https://en.wikipedia.org/wiki/Look-and-say_sequence. But in this soluition, letter comes first, count comes after that.
package hackoranko
object LookAndSay {
def LookAndSay(start: String, n: Int): String = {
var first = start(0)
var tmpStart = new StringBuilder(start(0).toString() + "1")
for (t <- start.tail) {
if (first.equals(t)) {
tmpStart(tmpStart.size - 1) = (tmpStart.last.toInt + 1).toChar
} else {
tmpStart.append(t + "1")
}
first = t
}
if ((n - 1) == 0) {
tmpStart.toString()
} else {
LookAndSay(tmpStart.toString(), n - 1)
}
}
def main(args: Array[String]) {
val start = "1"
val cycle = 6
println("Result: " + LookAndSay(start, cycle)) // -> 12221131
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment