Skip to content

Instantly share code, notes, and snippets.

@not-for-me
Last active June 22, 2017 08:48
Show Gist options
  • Save not-for-me/33ecf82edcd8f8bb34711a034372f230 to your computer and use it in GitHub Desktop.
Save not-for-me/33ecf82edcd8f8bb34711a034372f230 to your computer and use it in GitHub Desktop.
Simple LookAndSay Algorithm
// 0 is the first line from the lookAndSay result.
fun ant(line: Int): String {
var resultString = "1"
var lineCounter = line
while (lineCounter-- > 0) {
var tempBuffer = StringBuilder()
var curCompareChar = resultString[0]
var curCnt = 1
for (idx in 1..resultString.length - 1) {
if (curCompareChar == resultString[idx]) {
curCnt++
} else {
tempBuffer.append(curCompareChar).append(curCnt)
curCompareChar = resultString[idx]
curCnt = 1
}
}
tempBuffer.append(curCompareChar).append(curCnt)
resultString = tempBuffer.toString()
}
return resultString
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment