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
26. Using a key of
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 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.
The quick brown fox jumps over the lazy dog.gives
Gur dhvpx oebja sbk whzcf bire gur ynml qbt.
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:
To include color from the command line:
ruby -r minitest/pride rotational_cipher_test.rb
It's possible to submit an incomplete solution so you can see how others have completed the exercise.
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 attr_reader :string, :rotation 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
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.