Skip to content

Instantly share code, notes, and snippets.

@queimadus
queimadus / encoder.scala
Created Feb 29, 2020
Chuck Norris Unary Code Binary Encoder
View encoder.scala
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(" ")
}
You can’t perform that action at this time.