Last active
December 31, 2017 09:09
-
-
Save pfcoperez/f6a72f20ce18b6ae0a49699cd9c68d0c to your computer and use it in GitHub Desktop.
This file contains hidden or 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
object Caesar extends App { | |
private lazy val pos2symbol: Vector[Char] = (('A' to 'Z') ++ ('a' to 'z') :+ ' ' ).toVector | |
private lazy val symbol2pos: Map[Char, Int] = pos2symbol.zipWithIndex.toMap | |
def encode(msg: String)(implicit key: Int): String = | |
msg collect { case c if symbol2pos contains c => | |
pos2symbol((symbol2pos(c) + key) % symbol2pos.size) | |
} | |
def decode(msg: String)(implicit key: Int): String = | |
msg map { c => | |
val s = pos2symbol.size | |
pos2symbol((s + symbol2pos(c) - key) % s) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment