Last active
June 22, 2017 08:48
-
-
Save not-for-me/33ecf82edcd8f8bb34711a034372f230 to your computer and use it in GitHub Desktop.
Simple LookAndSay Algorithm
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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