Skip to content

Instantly share code, notes, and snippets.

@pfcoperez
Last active December 31, 2017 09:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pfcoperez/f6a72f20ce18b6ae0a49699cd9c68d0c to your computer and use it in GitHub Desktop.
Save pfcoperez/f6a72f20ce18b6ae0a49699cd9c68d0c to your computer and use it in GitHub Desktop.
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