Last active
December 20, 2015 17:59
-
-
Save bmarcot/6172620 to your computer and use it in GitHub Desktop.
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
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