Skip to content

Instantly share code, notes, and snippets.

@bmarcot
Last active December 20, 2015 17:59
Show Gist options
  • Save bmarcot/6172620 to your computer and use it in GitHub Desktop.
Save bmarcot/6172620 to your computer and use it in GitHub Desktop.
def split[A](chars: List[A], n: Int): List[List[A]] = {
if (chars.isEmpty) List()
else chars.take(n) :: split[A](chars.drop(n), n)
}
def encrypt_rec(chars: List[List[Char]]): List[List[Char]] = {
if (chars.isEmpty) Nil
else {
val cs = chars.filter(_.isEmpty == false)
cs.map(_.head) :: encrypt_rec(cs.map(_.tail))
}
}
def encrypt(s: String): String = {
val chars = s.toList.filter(_ != ' ')
val len = math.ceil(math.sqrt(chars.length)).toInt
encrypt_rec(split[Char](chars, len)).map(_.mkString).reduceLeft(_ + ' ' + _)
}
println(encrypt("if man was meant to stay on the ground god would have given us roots"))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment