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

Published at Jul 30 2020
·
0 comments

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.

- Encoding
`test`

gives`gvhg`

- Decoding
`gvhg`

gives`test`

- Decoding
`gsvjf rxpyi ldmul cqfnk hlevi gsvoz abwlt`

gives`thequickbrownfoxjumpsoverthelazydog`

For installation and learning resources, refer to the Ruby resources page.

For running the tests provided, you will need the Minitest gem. Open a terminal window and run the following command to install minitest:

```
gem install minitest
```

If you would like color output, you can `require 'minitest/pride'`

in
the test file, or note the alternative instruction, below, for running
the test file.

Run the tests from the exercise directory using the following command:

```
ruby atbash_cipher_test.rb
```

To include color from the command line:

```
ruby -r minitest/pride atbash_cipher_test.rb
```

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

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

```
require 'minitest/autorun'
require_relative 'atbash_cipher'
# Common test data version: 1.2.0 d5238bd
class AtbashCipherTest < Minitest::Test
def test_encode_yes
# skip
plaintext = 'yes'
ciphertext = 'bvh'
assert_equal ciphertext, Atbash.encode(plaintext)
end
def test_encode_no
skip
plaintext = 'no'
ciphertext = 'ml'
assert_equal ciphertext, Atbash.encode(plaintext)
end
def test_encode_omg
skip
plaintext = 'OMG'
ciphertext = 'lnt'
assert_equal ciphertext, Atbash.encode(plaintext)
end
def test_encode_spaces
skip
plaintext = 'O M G'
ciphertext = 'lnt'
assert_equal ciphertext, Atbash.encode(plaintext)
end
def test_encode_mindblowingly
skip
plaintext = 'mindblowingly'
ciphertext = 'nrmwy oldrm tob'
assert_equal ciphertext, Atbash.encode(plaintext)
end
def test_encode_numbers
skip
plaintext = 'Testing,1 2 3, testing.'
ciphertext = 'gvhgr mt123 gvhgr mt'
assert_equal ciphertext, Atbash.encode(plaintext)
end
def test_encode_deep_thought
skip
plaintext = 'Truth is fiction.'
ciphertext = 'gifgs rhurx grlm'
assert_equal ciphertext, Atbash.encode(plaintext)
end
def test_encode_all_the_letters
skip
plaintext = 'The quick brown fox jumps over the lazy dog.'
ciphertext = 'gsvjf rxpyi ldmul cqfnk hlevi gsvoz abwlt'
assert_equal ciphertext, Atbash.encode(plaintext)
end
def test_decode_exercism
skip
ciphertext = 'vcvix rhn'
plaintext = 'exercism'
assert_equal plaintext, Atbash.decode(ciphertext)
end
def test_decode_a_sentence
skip
ciphertext = 'zmlyh gzxov rhlug vmzhg vkkrm thglm v'
plaintext = 'anobstacleisoftenasteppingstone'
assert_equal plaintext, Atbash.decode(ciphertext)
end
def test_decode_numbers
skip
ciphertext = 'gvhgr mt123 gvhgr mt'
plaintext = 'testing123testing'
assert_equal plaintext, Atbash.decode(ciphertext)
end
def test_decode_all_the_letters
skip
ciphertext = 'gsvjf rxpyi ldmul cqfnk hlevi gsvoz abwlt'
plaintext = 'thequickbrownfoxjumpsoverthelazydog'
assert_equal plaintext, Atbash.decode(ciphertext)
end
def test_decode_with_too_many_spaces
skip
ciphertext = 'vc vix r hn'
plaintext = 'exercism'
assert_equal plaintext, Atbash.decode(ciphertext)
end
def test_decode_with_no_spaces
skip
ciphertext = 'zmlyhgzxovrhlugvmzhgvkkrmthglmv'
plaintext = 'anobstacleisoftenasteppingstone'
assert_equal plaintext, Atbash.decode(ciphertext)
end
end
```

```
=begin
Write your code for the 'Atbash Cipher' exercise in this file. Make the tests in
`atbash_cipher_test.rb` pass.
To get started with TDD, see the `README.md` file in your
`ruby/atbash-cipher` directory.
=end
module Atbash
def self.encode(plaintext)
plaintext
.downcase
.gsub(/[^a-z0-9]/, '')
.tr(('a'..'z').to_a.join, ('a'..'z').to_a.reverse.join)
.chars
.each_slice(5)
.map(&:join)
.join(' ')
end
def self.decode(ciphertext)
encode(ciphertext).gsub(' ', '')
end
end
```

A huge amount can be learned from reading other peopleâ€™s code. This is why we wanted to give exercism users the option of making their solutions public.

Here are some questions to help you reflect on this solution and learn the most from it.

- What compromises have been made?
- Are there new concepts here that you could read more about to improve your understanding?

Level up your programming skills with 3,428 exercises across 52 languages, and insightful discussion with our volunteer team of welcoming mentors.
Exercism is
**100% free forever**.

## Community comments