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

# Mahesh12's solution

## to Rotational Cipher in the Ruby Track

Published at Sep 30 2020 · 0 comments
Instructions
Test suite
Solution

Create an implementation of the rotational cipher, also sometimes called the Caesar cipher.

The Caesar cipher is a simple shift cipher that relies on transposing all the letters in the alphabet using an integer key between `0` and `26`. Using a key of `0` or `26` will always yield the same output due to modular arithmetic. The letter is shifted for as many values as the value of the key.

The general notation for rotational ciphers is `ROT + <key>`. The most commonly used rotational cipher is `ROT13`.

A `ROT13` on the Latin alphabet would be as follows:

``````Plain:  abcdefghijklmnopqrstuvwxyz
Cipher: nopqrstuvwxyzabcdefghijklm
``````

It is stronger than the Atbash cipher because it has 27 possible keys, and 25 usable keys.

Ciphertext is written out in the same formatting as the input including spaces and punctuation.

## Examples

• ROT5 `omg` gives `trl`
• ROT0 `c` gives `c`
• ROT26 `Cool` gives `Cool`
• ROT13 `The quick brown fox jumps over the lazy dog.` gives `Gur dhvpx oebja sbk whzcf bire gur ynml qbt.`
• ROT13 `Gur dhvpx oebja sbk whzcf bire gur ynml qbt.` gives `The quick brown fox jumps over the lazy dog.`

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 rotational_cipher_test.rb
``````

To include color from the command line:

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

## Submitting Incomplete Solutions

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

### rotational_cipher_test.rb

``````require 'minitest/autorun'
require_relative 'rotational_cipher'

# Common test data version: 1.2.0 cf23851
class RotationalCipherTest < Minitest::Test
def test_rotate_a_by_0_same_output_as_input
# skip
assert_equal "a", RotationalCipher.rotate("a", 0)
end

def test_rotate_a_by_1
skip
assert_equal "b", RotationalCipher.rotate("a", 1)
end

def test_rotate_a_by_26_same_output_as_input
skip
assert_equal "a", RotationalCipher.rotate("a", 26)
end

def test_rotate_m_by_13
skip
assert_equal "z", RotationalCipher.rotate("m", 13)
end

def test_rotate_n_by_13_with_wrap_around_alphabet
skip
assert_equal "a", RotationalCipher.rotate("n", 13)
end

def test_rotate_capital_letters
skip
assert_equal "TRL", RotationalCipher.rotate("OMG", 5)
end

def test_rotate_spaces
skip
assert_equal "T R L", RotationalCipher.rotate("O M G", 5)
end

def test_rotate_numbers
skip
assert_equal "Xiwxmrk 1 2 3 xiwxmrk", RotationalCipher.rotate("Testing 1 2 3 testing", 4)
end

def test_rotate_punctuation
skip
assert_equal "Gzo'n zvo, Bmviyhv!", RotationalCipher.rotate("Let's eat, Grandma!", 21)
end

def test_rotate_all_letters
skip
assert_equal "Gur dhvpx oebja sbk whzcf bire gur ynml qbt.", RotationalCipher.rotate("The quick brown fox jumps over the lazy dog.", 13)
end
end``````
``````class RotationalCipher
ALPHABET = 'abcdefghijklmnopqrstuvwxyz'

def self.rotate(string, rotation)
new(string, rotation).rotate
end

def initialize(string, rotation)
@string = string
@rotation = rotation
end

def rotate
string.each_char.map do |char|
old_index = ALPHABET.index(char.downcase)
new_char = old_index ? ALPHABET[(old_index + rotation) % 26] : char
new_char.upcase! if upcase?(char)
new_char
end.join
end

private

def upcase?(char)
char.upcase == char
end
end``````