Avatar of paulfioravanti

paulfioravanti's solution

to Rotational Cipher in the Ruby Track

Published at May 09 2019 · 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

Source

Wikipedia https://en.wikipedia.org/wiki/Caesar_cipher

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
# frozen_string_literal: true

module RotationalCipher
  ALPHABET_LENGTH = 26
  private_constant :ALPHABET_LENGTH
  ALPHA_CHARACTER = /[A-Za-z]/.freeze
  private_constant :ALPHA_CHARACTER
  DEFAULT_BASE = 0
  private_constant :DEFAULT_BASE
  INITIAL_CIPHER = ""
  private_constant :INITIAL_CIPHER
  IS_LOWERCASE = ->(char) { char.between?("a", "z") }
  private_constant :IS_LOWERCASE
  IS_UPPERCASE = ->(char) { char.between?("A", "Z") }
  private_constant :IS_UPPERCASE
  LOWER_BASE = "a".ord
  private_constant :LOWER_BASE
  UPPER_BASE = "A".ord
  private_constant :UPPER_BASE

  module_function

  def rotate(string, key)
    string.chars.reduce(INITIAL_CIPHER) do |acc, char|
      next (acc + char) unless char.match?(ALPHA_CHARACTER)

      acc + rotate_character(char, key)
    end
  end

  def rotate_character(char, key)
    base = base_for(char)
    char
      .ord
      .then { |num| num - base + key }
      .modulo(ALPHABET_LENGTH)
      .then { |num| num + base }
      .chr
  end
  private_class_method :rotate_character

  def base_for(char)
    case char
    when IS_LOWERCASE
      LOWER_BASE
    when IS_UPPERCASE
      UPPER_BASE
    else
      DEFAULT_BASE
    end
  end
  private_class_method :base_for
end

Community comments

Find this solution interesting? Ask the author a question to learn more.

What can you learn from this solution?

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?