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

# 4d47's solution

## to Rail Fence Cipher in the PHP 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 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.

## Running the tests

1. Go to the root of your PHP exercise directory, which is `<EXERCISM_WORKSPACE>/php`. To find the Exercism workspace run

`````` % exercism debug | grep Workspace
``````
2. Get PHPUnit if you don't have it already.

`````` % wget --no-check-certificate https://phar.phpunit.de/phpunit.phar
% chmod +x phpunit.phar
``````
3. Execute the tests:

`````` % ./phpunit.phar rail-fence-cipher/rail-fence-cipher_test.php
``````

## 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.php

``````<?php
require_once "rail-fence-cipher.php";

class RailFenceCipherTest extends PHPUnit\Framework\TestCase
{

/**
* Test encode with two rails.
*/
public function testEncodeWithTwoRails()
{
\$plainText  = "XOXOXOXOXOXOXOXOXO";
\$rails = 2;
\$expected = "XXXXXXXXXOOOOOOOOO";
\$this->assertEquals(encode(\$plainText, \$rails), \$expected);
}
/**
* Test encode with three rails.
*/
public function testEncodeWithThreeRails()
{
\$plainText  = "WEAREDISCOVEREDFLEEATONCE";
\$rails = 3;
\$expected = "WECRLTEERDSOEEFEAOCAIVDEN";
\$this->assertEquals(encode(\$plainText, \$rails), \$expected);
}

/**
* Test encode with ending in the middle.
*/
public function testEncodeWithEndingInTheMiddle()
{
\$plainText  = "EXERCISES";
\$rails = 4;
\$expected = "ESXIEECSR";
\$this->assertEquals(encode(\$plainText, \$rails), \$expected);
}

/**
* Test decode with three rails.
*/
public function testDecodeWithThreeRails()
{
\$encryptedText  = "TEITELHDVLSNHDTISEIIEA";
\$rails = 3;
\$expected = "THEDEVILISINTHEDETAILS";
\$this->assertEquals(decode(\$encryptedText, \$rails), \$expected);
}
/**
* Test decode with five rails.
*/
public function testDecodeWithFiveRails()
{
\$encryptedText  = "EIEXMSMESAORIWSCE";
\$rails = 5;
\$expected = "EXERCISMISAWESOME";
\$this->assertEquals(decode(\$encryptedText, \$rails), \$expected);
}

/**
* Test decode with six rails.
*/
public function testDecodeWithSixRails()
{
\$encryptedText  = "133714114238148966225439541018335470986172518171757571896261";
\$rails = 6;
\$expected = "112358132134558914423337761098715972584418167651094617711286";
\$this->assertEquals(decode(\$encryptedText, \$rails), \$expected);
}
}``````
``````<?php

function encode(string \$plain, int \$height): string {
assert(\$height > 1);
\$rails = array_fill(0, \$height, []);
\$index = 0;
\$sign = +1;
foreach (str_split(\$plain) as \$i => \$c) {
\$rails[\$index][\$i] = \$c;
inc_rail(\$index, \$sign, \$rails);
}
return implode(array_map('implode', \$rails));
}

function decode(string \$ciphertext, int \$height): string {
assert(\$height > 1);
\$result = [];
\$rails = [];
\$index = 0;
\$sign = +1;
\$letters = str_split(\$ciphertext);
\$width = count(\$letters) / (\$height * 2 - 2);
\$border = ceil(\$width);
\$middle = floor(\$width * 2);

for (\$i = \$offset = 0; \$i < \$height; \$i++) {
\$w = (\$i === 0 || \$i === \$height - 1) ? \$border : \$middle;
\$rails[] = array_slice(\$letters, \$offset, \$w);
\$offset += \$w;
}
while (count(\$result) < count(\$letters)) {
\$result[] = array_shift(\$rails[\$index]);
inc_rail(\$index, \$sign, \$rails);
}
return implode(\$result);
}

function inc_rail(&\$index, &\$sign, &\$rails) {
if (\$index === 0) {
\$sign = +1;
} elseif (\$index === count(\$rails) - 1) {
\$sign = -1;
}
\$index += 1 * \$sign;
}``````