ðŸŽ‰ Exercism Research is now launched. Help Exercism, help science and have some fun at research.exercism.io ðŸŽ‰

# SergiiVlasiuk's solution

## to Atbash Cipher in the Scala Track

Published at Aug 28 2019 · 1 comment
Instructions
Test suite
Solution

Create an implementation of the atbash cipher, an ancient encryption system created in the Middle East.

The Atbash cipher is a simple substitution cipher that relies on transposing all the letters in the alphabet such that the resulting alphabet is backwards. The first letter is replaced with the last letter, the second with the second-last, and so on.

An Atbash cipher for the Latin alphabet would be as follows:

``````Plain:  abcdefghijklmnopqrstuvwxyz
Cipher: zyxwvutsrqponmlkjihgfedcba
``````

It is a very weak cipher because it only has one possible key, and it is a simple monoalphabetic substitution cipher. However, this may not have been an issue in the cipher's time.

Ciphertext is written out in groups of fixed length, the traditional group size being 5 letters, and punctuation is excluded. This is to make it harder to guess things based on word boundaries.

## Examples

• Encoding `test` gives `gvhg`
• Decoding `gvhg` gives `test`
• Decoding `gsvjf rxpyi ldmul cqfnk hlevi gsvoz abwlt` gives `thequickbrownfoxjumpsoverthelazydog`

The Scala exercises assume an SBT project scheme. The exercise solution source should be placed within the exercise directory/src/main/scala. The exercise unit tests can be found within the exercise directory/src/test/scala.

To run the tests simply run the command `sbt test` in the exercise directory.

For more detailed info about the Scala track see the help page.

## Source

Wikipedia http://en.wikipedia.org/wiki/Atbash

## Submitting Incomplete Solutions

It's possible to submit an incomplete solution so you can see how others have completed the exercise.

### AtbashCipherTest.scala

``````import org.scalatest.{Matchers, FunSuite}

/** @version 1.2.0 */
class AtbashCipherTest extends FunSuite with Matchers {

test("encode yes") {
AtbashCipher.encode("yes") should be("bvh")
}

test("encode no") {
pending
AtbashCipher.encode("no") should be("ml")
}

test("encode OMG") {
pending
AtbashCipher.encode("OMG") should be("lnt")
}

test("encode spaces") {
pending
AtbashCipher.encode("O M G") should be("lnt")
}

test("encode mindblowingly") {
pending
AtbashCipher.encode("mindblowingly") should be("nrmwy oldrm tob")
}

test("encode numbers") {
pending
AtbashCipher.encode("Testing,1 2 3, testing.") should be(
"gvhgr mt123 gvhgr mt")
}

test("encode deep thought") {
pending
AtbashCipher.encode("Truth is fiction.") should be("gifgs rhurx grlm")
}

test("encode all the letters") {
pending
AtbashCipher.encode("The quick brown fox jumps over the lazy dog.") should be(
"gsvjf rxpyi ldmul cqfnk hlevi gsvoz abwlt")
}

test("decode exercism") {
pending
AtbashCipher.decode("vcvix rhn") should be("exercism")
}

test("decode a sentence") {
pending
AtbashCipher.decode("zmlyh gzxov rhlug vmzhg vkkrm thglm v") should be(
"anobstacleisoftenasteppingstone")
}

test("decode numbers") {
pending
AtbashCipher.decode("gvhgr mt123 gvhgr mt") should be("testing123testing")
}

test("decode all the letters") {
pending
AtbashCipher.decode("gsvjf rxpyi ldmul cqfnk hlevi gsvoz abwlt") should be(
"thequickbrownfoxjumpsoverthelazydog")
}

test("decode with too many spaces") {
pending
AtbashCipher.decode("vc vix    r hn") should be("exercism")
}

test("decode with no spaces") {
pending
AtbashCipher.decode("zmlyhgzxovrhlugvmzhgvkkrmthglmv") should be(
"anobstacleisoftenasteppingstone")
}
}``````
``````object AtbashCipher {
val pairs = {
val alphabet = 'a' to 'z'
alphabet.zip(alphabet.reverse).toMap
}

def decode(input: String): String = input
.replaceAll(""" |\+|\p{P}""", "")
.toLowerCase
.map(c => pairs.getOrElse(c, c))

def encode(phrase: String): String = decode(phrase).grouped(5).mkString(" ")
}``````

Solution Author

one more solution, simpler than first one:

``````object AtbashCipher {
def encode(sentence: String): String = decode(sentence.toLowerCase)
.grouped(5)
.mkString(" ")

def decode: String => String = _.replaceAll(""" |\+|\p{P}""", "").map {
case c if c.isLetter => ('z' - (c - 'a')).toChar
case notLetter => notLetter
}
}
``````