# fbraza's solution

## to Rail Fence Cipher in the Ruby Track

Published at Aug 30 2019 · 0 comments
Instructions
Test suite
Solution

Implement encoding and decoding for the rail fence cipher.

The Rail Fence cipher is a form of transposition cipher that gets its name from the way in which it's encoded. It was already used by the ancient Greeks.

In the Rail Fence cipher, the message is written downwards on successive "rails" of an imaginary fence, then moving up when we get to the bottom (like a zig-zag). Finally the message is then read off in rows.

For example, using three "rails" and the message "WE ARE DISCOVERED FLEE AT ONCE", the cipherer writes out:

``````W . . . E . . . C . . . R . . . L . . . T . . . E
. E . R . D . S . O . E . E . F . E . A . O . C .
. . A . . . I . . . V . . . D . . . E . . . N . .
``````

``````WECRLTEERDSOEEFEAOCAIVDEN
``````

To decrypt a message you take the zig-zag shape and fill the ciphertext along the rows.

``````? . . . ? . . . ? . . . ? . . . ? . . . ? . . . ?
. ? . ? . ? . ? . ? . ? . ? . ? . ? . ? . ? . ? .
. . ? . . . ? . . . ? . . . ? . . . ? . . . ? . .
``````

The first row has seven spots that can be filled with "WECRLTE".

``````W . . . E . . . C . . . R . . . L . . . T . . . E
. ? . ? . ? . ? . ? . ? . ? . ? . ? . ? . ? . ? .
. . ? . . . ? . . . ? . . . ? . . . ? . . . ? . .
``````

Now the 2nd row takes "ERDSOEEFEAOC".

``````W . . . E . . . C . . . R . . . L . . . T . . . E
. E . R . D . S . O . E . E . F . E . A . O . C .
. . ? . . . ? . . . ? . . . ? . . . ? . . . ? . .
``````

Leaving "AIVDEN" for the last row.

``````W . . . E . . . C . . . R . . . L . . . T . . . E
. E . R . D . S . O . E . E . F . E . A . O . C .
. . A . . . I . . . V . . . D . . . E . . . N . .
``````

If you now read along the zig-zag shape you can read the original message.

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

To include color from the command line:

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

## Submitting Incomplete Solutions

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

### rail_fence_cipher_test.rb

``````require 'minitest/autorun'
require_relative 'rail_fence_cipher'

class RailFenceCipherTest < Minitest::Test
def test_encode_with_empty_string
assert_equal '', RailFenceCipher.encode('', 4)
end

def test_encode_with_one_rail
skip
assert_equal 'One rail, only one rail',
RailFenceCipher.encode('One rail, only one rail', 1)
end

def test_encode_with_two_rails
skip
assert_equal 'XXXXXXXXXOOOOOOOOO',
RailFenceCipher.encode('XOXOXOXOXOXOXOXOXO', 2)
end

def test_encode_with_three_rails
skip
assert_equal 'WECRLTEERDSOEEFEAOCAIVDEN',
RailFenceCipher.encode('WEAREDISCOVEREDFLEEATONCE', 3)
end

def test_encode_with_ending_in_the_middle
skip
assert_equal 'ESXIEECSR', RailFenceCipher.encode('EXERCISES', 4)
end

def test_encode_with_less_letters_than_rails
skip
assert_equal 'More rails than letters',
RailFenceCipher.encode('More rails than letters', 24)
end

def test_decode_with_empty_string
skip
assert_equal '', RailFenceCipher.decode('', 4)
end

def test_decode_with_one_rail
skip
assert_equal 'ABCDEFGHIJKLMNOP',
RailFenceCipher.decode('ABCDEFGHIJKLMNOP', 1)
end

def test_decode_with_two_rails
skip
assert_equal 'XOXOXOXOXOXOXOXOXO',
RailFenceCipher.decode('XXXXXXXXXOOOOOOOOO', 2)
end

def test_decode_with_three_rails
skip
assert_equal 'THEDEVILISINTHEDETAILS',
RailFenceCipher.decode('TEITELHDVLSNHDTISEIIEA', 3)
end
end``````
``````class RailFenceCipher
def self.encode(message, rails_number)
cipher = new(message, rails_number)

cipher.unappropriate_rails_number? ? message : cipher.encode
end

def self.decode(message, rails_number)
cipher = new(message, rails_number)

cipher.unappropriate_rails_number? ? message : cipher.decode
end

def initialize(message, rails_number)
@message           = message
@rails             = rails_number
@encoding_iterator = 0
@decoding_iterator = 0
end

def encode
to_encode = message.chars
cipher    = Array.new(rails) { [] }
cipher[encoding_iterator] << to_encode.shift

until to_encode.empty?
encode_up_to_down(encoding_iterator, cipher, to_encode)
encode_down_to_top(encoding_iterator, cipher, to_encode)
end

cipher
.map(&:join)
.join
end

def decode
cipher = generate_decoding_rails_cipher
cipher
.transpose
.each { |nested| nested.delete('.') }
.join
end

def unappropriate_rails_number?
rails == 1 || rails > message.size
end

protected

def encode_up_to_down(encoding_iterator, cipher, to_encode)
(encoding_iterator + 1...rails).each do |idx|
self.encoding_iterator = idx
cipher[idx] << to_encode.shift
end
end

def encode_down_to_top(encoding_iterator, cipher, to_encode)
(0..encoding_iterator - 1).reverse_each do |idx|
self.encoding_iterator = idx
cipher[idx] << to_encode.shift
end
end

def generate_decoding_rails_cipher
to_decode   = message.chars
cipher      = Array.new(rails) { [] }
number_dots = 2 * (rails - 2) + 1

until to_decode.empty?
flag = false
count = 0
cipher[decoding_iterator] << to_decode.shift
if same_size?(cipher[decoding_iterator], message)
flag        = true
number_dots = update_number_of_dots(number_dots, rails)
update_decoding_iterator
return cipher if decoding_iterator == rails

decoding_iterator.times { cipher[decoding_iterator] << '.' }
count += decoding_iterator
end

while count <= number_dots - 1
cipher[decoding_iterator] << '.' unless flag
count += 1
next unless same_size?(cipher[decoding_iterator], message)

number_dots = update_number_of_dots(number_dots, rails)
update_decoding_iterator
return cipher if decoding_iterator == rails

decoding_iterator.times { cipher[decoding_iterator] << '.' }
count += decoding_iterator
end
end
end

def same_size?(object1, object2)
object1.size == object2.size
end

def update_number_of_dots(dots, rails)
(dots - 2).negative? ? 2 * (rails - 2) + 1 : dots - 2
end

def update_decoding_iterator
self.decoding_iterator += 1
end