Avatar of w1zeman1p

w1zeman1p's solution

to Crypto Square in the Ruby Track

Published at Jul 13 2018 · 0 comments
Instructions
Test suite
Solution

Note:

This solution was written on an old version of Exercism. The tests below might not correspond to the solution code, and the exercise may have changed since this code was written.

Implement the classic method for composing secret messages called a square code.

Given an English text, output the encoded version of that text.

First, the input is normalized: the spaces and punctuation are removed from the English text and the message is downcased.

Then, the normalized characters are broken into rows. These rows can be regarded as forming a rectangle when printed with intervening newlines.

For example, the sentence

If man was meant to stay on the ground, god would have given us roots.

is normalized to:

ifmanwasmeanttostayonthegroundgodwouldhavegivenusroots

The plaintext should be organized in to a rectangle. The size of the rectangle (r x c) should be decided by the length of the message, such that c >= r and c - r <= 1, where c is the number of columns and r is the number of rows.

Our normalized text is 54 characters long, dictating a rectangle with c = 8 and r = 7:

ifmanwas
meanttos
tayonthe
groundgo
dwouldha
vegivenu
sroots

The coded message is obtained by reading down the columns going left to right.

The message above is coded as:

imtgdvsfearwermayoogoanouuiontnnlvtwttddesaohghnsseoau

Output the encoded text in chunks. Phrases that fill perfect rectangles (r X c) should be output c chunks of r length, separated by spaces. Phrases that do not fill perfect rectangles will have n empty spaces. Those spaces should be distributed evenly, added to the end of the last n chunks.

imtgdvs fearwer mayoogo anouuio ntnnlvt wttddes aohghn  sseoau 

Notice that were we to stack these, we could visually decode the cyphertext back in to the original message:

imtgdvs
fearwer
mayoogo
anouuio
ntnnlvt
wttddes
aohghn
sseoau

For installation and learning resources, refer to the exercism help 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 crypto_square_test.rb

To include color from the command line:

ruby -r minitest/pride crypto_square_test.rb

Source

J Dalbey's Programming Practice problems http://users.csc.calpoly.edu/~jdalbey/103/Projects/ProgrammingPractice.html

Submitting Incomplete Solutions

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

crypto_square_test.rb

require 'minitest/autorun'
require_relative 'crypto_square'

# Common test data version: 3.1.0 e937744
class CryptoSquareTest < Minitest::Test
  def test_empty_plaintext_results_in_an_empty_ciphertext
    # skip
    plaintext = ''
    assert_equal "", Crypto.new(plaintext).ciphertext
  end

  def test_lowercase
    skip
    plaintext = 'A'
    assert_equal "a", Crypto.new(plaintext).ciphertext
  end

  def test_remove_spaces
    skip
    plaintext = '  b '
    assert_equal "b", Crypto.new(plaintext).ciphertext
  end

  def test_remove_punctuation
    skip
    plaintext = '@1,%!'
    assert_equal "1", Crypto.new(plaintext).ciphertext
  end

  def test_9_character_plaintext_results_in_3_chunks_of_3_characters
    skip
    plaintext = 'This is fun!'
    assert_equal "tsf hiu isn", Crypto.new(plaintext).ciphertext
  end

  def test_8_character_plaintext_results_in_3_chunks_the_last_one_with_a_trailing_space
    skip
    plaintext = 'Chill out.'
    assert_equal "clu hlt io ", Crypto.new(plaintext).ciphertext
  end

  def test_54_character_plaintext_results_in_7_chunks_the_last_two_with_trailing_spaces
    skip
    plaintext = 'If man was meant to stay on the ground, god would have given us roots.'
    assert_equal "imtgdvs fearwer mayoogo anouuio ntnnlvt wttddes aohghn  sseoau ", Crypto.new(plaintext).ciphertext
  end

  # Problems in exercism evolve over time, as we find better ways to ask
  # questions.
  # The version number refers to the version of the problem you solved,
  # not your solution.
  #
  # Define a constant named VERSION inside of the top level BookKeeping
  # module, which may be placed near the end of your file.
  #
  # In your file, it will look like this:
  #
  # module BookKeeping
  #   VERSION = 1 # Where the version number matches the one in the test.
  # end
  #
  # If you are curious, read more about constants on RubyDoc:
  # http://ruby-doc.org/docs/ruby-doc-bundle/UsersGuide/rg/constants.html

  def test_bookkeeping
    skip
    assert_equal 1, BookKeeping::VERSION
  end
end
class Crypto
  def initialize(input)
    @input = input
  end

  def normalize_plaintext
    input.downcase.gsub(/[^a-z0-9]/, '')
  end

  def size
    Math.sqrt(normalize_plaintext.length).ceil
  end

  def plaintext_segments
    normalize_plaintext
      .chars
      .each_slice(size)
      .map(&:join)
  end

  def ciphertext
    ciphertext_segments.join
  end

  def normalize_ciphertext
    ciphertext_segments.join(' ')
  end

  private

  attr_reader :input

  def ciphertext_segments
    result = Array.new(size) { [] }
    plaintext_segments.each do |segment|
      segment.chars.each_with_index do |char, j|
        result[j] << char
      end
    end
    result.map(&:join)
  end
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?