Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Chuck Norris Unary Code Binary Encoder
def encodeBinaryString(s: String) = {
s.foldLeft(List.empty[List[Char]]) {
case (List(), cur) => List(List(cur))
case ((head @ h :: _) :: tail, cur) if cur == h => (cur :: head) :: tail
case (acc, cur) => List(cur) :: acc
}.map {
case l @ '1' :: _ => "0 " + "0" * l.size
case l @ '0' :: _ => "00 " + "0" * l.size
}.mkString(" ")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment