Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -540,6 +540,14 @@
"prerequisites": [],
"difficulty": 5
},
{
"slug": "rail-fence-cipher",
"name": "Rail Fence Cipher",
"uuid": "252f263c-9a4a-4c18-8c23-0a1fcc94b36a",
"practices": [],
"prerequisites": [],
"difficulty": 6
},
{
"slug": "connect",
"name": "Connect",
Expand Down
57 changes: 57 additions & 0 deletions exercises/practice/rail-fence-cipher/.docs/instructions.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
# Instructions

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:

```text
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 . .
```

Then reads off:

```text
WECRLTEERDSOEEFEAOCAIVDEN
```

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

```text
? . . . ? . . . ? . . . ? . . . ? . . . ? . . . ?
. ? . ? . ? . ? . ? . ? . ? . ? . ? . ? . ? . ? .
. . ? . . . ? . . . ? . . . ? . . . ? . . . ? . .
```

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

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

Now the 2nd row takes "ERDSOEEFEAOC".

```text
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.

```text
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.
19 changes: 19 additions & 0 deletions exercises/practice/rail-fence-cipher/.meta/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"authors": [
"keiravillekode"
],
"files": {
"solution": [
"RailFenceCipher.pas"
],
"test": [
"TestCases.pas"
],
"example": [
".meta/example.pas"
]
},
"blurb": "Implement encoding and decoding for the rail fence cipher.",
"source": "Wikipedia",
"source_url": "https://en.wikipedia.org/wiki/Transposition_cipher#Rail_Fence_cipher"
}
75 changes: 75 additions & 0 deletions exercises/practice/rail-fence-cipher/.meta/example.pas
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
unit RailFenceCipher;

{$mode ObjFPC}{$H+}

interface

function encode(const msg : String; const rails : Smallint) : String;
function decode(const msg : String; const rails : Smallint) : String;

implementation

uses SysUtils;

function cipher(const msg : String; const rails : Smallint; decipher: Boolean) : String;
var
table : Array of Integer = ();
rail : Integer;
direction : Integer;
acc : Integer;
entry : Integer;
index : Integer;
begin
SetLength(table, rails);
for rail := 0 to rails - 1 do
table[rail] := 0;

rail := 0;
direction := 1;
for index := 1 to Length(msg) do
begin
entry := table[rail] + 1;
table[rail] := entry;
rail := rail + direction;
if (rail = 0) or (rail + 1 = rails) then
direction := -direction;
end;

acc := 0;
for rail := 0 to rails - 1 do
begin
entry := table[rail];
table[rail] := acc;
acc := acc + entry;
end;

result := '';
SetLength(result, Length(msg));

rail := 0;
direction := 1;
for index := 1 to Length(msg) do
begin
entry := table[rail] + 1;
if decipher then
result[index] := msg[entry]
else
result[entry] := msg[index];
table[rail] := entry;
rail := rail + direction;
if (rail = 0) or (rail + 1 = rails) then
direction := -direction;
end;
end;

function encode(const msg : String; const rails : Smallint) : String;
begin
result := cipher(msg, rails, false);
end;

function decode(const msg : String; const rails : Smallint) : String;
begin
result := cipher(msg, rails, true);
end;

end.
28 changes: 28 additions & 0 deletions exercises/practice/rail-fence-cipher/.meta/tests.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# This is an auto-generated file.
#
# Regenerating this file via `configlet sync` will:
# - Recreate every `description` key/value pair
# - Recreate every `reimplements` key/value pair, where they exist in problem-specifications
# - Remove any `include = true` key/value pair (an omitted `include` key implies inclusion)
# - Preserve any other key/value pair
#
# As user-added comments (using the # character) will be removed when this file
# is regenerated, comments can be added via a `comment` key.

[46dc5c50-5538-401d-93a5-41102680d068]
description = "encode -> encode with two rails"

[25691697-fbd8-4278-8c38-b84068b7bc29]
description = "encode -> encode with three rails"

[384f0fea-1442-4f1a-a7c4-5cbc2044002c]
description = "encode -> encode with ending in the middle"

[cd525b17-ec34-45ef-8f0e-4f27c24a7127]
description = "decode -> decode with three rails"

[dd7b4a98-1a52-4e5c-9499-cbb117833507]
description = "decode -> decode with five rails"

[93e1ecf4-fac9-45d9-9cd2-591f47d3b8d3]
description = "decode -> decode with six rails"
15 changes: 15 additions & 0 deletions exercises/practice/rail-fence-cipher/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
SHELL = /bin/bash
MAKEFLAGS += --no-print-directory
DESTDIR = build
EXECUTABLE = $(DESTDIR)/test
COMMAND = fpc -l- -v0 -g -gl -Sa -Cr -Sehnw -Fu./lib test.pas -FE"./$(DESTDIR)"

.ONESHELL:

test:
@mkdir -p "./$(DESTDIR)"
@cp -r ./lib "./$(DESTDIR)"
@$(COMMAND) && ./$(EXECUTABLE) $(test)

clean:
@rm -fr "./$(DESTDIR)"
28 changes: 28 additions & 0 deletions exercises/practice/rail-fence-cipher/RailFenceCipher.pas
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
unit RailFenceCipher;

{$mode ObjFPC}{$H+}

interface

function encode(const msg : String; const rails : Smallint) : String;
function decode(const msg : String; const rails : Smallint) : String;

implementation

uses SysUtils;

function encode(const msg : String; const rails : Smallint) : String;
begin

raise ENotImplemented.Create('Please implement your solution.'); result := Copy(msg, 0, rails);

end;

function decode(const msg : String; const rails : Smallint) : String;
begin

raise ENotImplemented.Create('Please implement your solution.'); result := Copy(msg, 0, rails);

end;

end.
81 changes: 81 additions & 0 deletions exercises/practice/rail-fence-cipher/TestCases.pas
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
unit TestCases;

{$mode ObjFPC}{$H+}

interface

uses Classes, SysUtils, FPCUnit, TestRegistry, FPCUnitTestUtils;

type
RailFenceCipherTest = class(TTestCase)
published
procedure encode_with_two_rails;
procedure encode_with_three_rails;
procedure encode_with_ending_in_the_middle;
procedure decode_with_three_rails;
procedure decode_with_five_rails;
procedure decode_with_six_rails;
end;

implementation

uses RailFenceCipher;

// 46dc5c50-5538-401d-93a5-41102680d068
procedure RailFenceCipherTest.encode_with_two_rails;
const
phrase : String = 'XOXOXOXOXOXOXOXOXO';
expect : String = 'XXXXXXXXXOOOOOOOOO';
begin
TapAssertTrue(Self, 'encode with two rails', expect, RailFenceCipher.encode(phrase, 2));
end;

// 25691697-fbd8-4278-8c38-b84068b7bc29
procedure RailFenceCipherTest.encode_with_three_rails;
const
phrase : String = 'WEAREDISCOVEREDFLEEATONCE';
expect : String = 'WECRLTEERDSOEEFEAOCAIVDEN';
begin
TapAssertTrue(Self, 'encode with three rails', expect, RailFenceCipher.encode(phrase, 3));
end;

// 384f0fea-1442-4f1a-a7c4-5cbc2044002c
procedure RailFenceCipherTest.encode_with_ending_in_the_middle;
const
phrase : String = 'EXERCISES';
expect : String = 'ESXIEECSR';
begin
TapAssertTrue(Self, 'encode with ending in the middle', expect, RailFenceCipher.encode(phrase, 4));
end;

// cd525b17-ec34-45ef-8f0e-4f27c24a7127
procedure RailFenceCipherTest.decode_with_three_rails;
const
phrase : String = 'TEITELHDVLSNHDTISEIIEA';
expect : String = 'THEDEVILISINTHEDETAILS';
begin
TapAssertTrue(Self, 'decode with three rails', expect, RailFenceCipher.decode(phrase, 3));
end;

// dd7b4a98-1a52-4e5c-9499-cbb117833507
procedure RailFenceCipherTest.decode_with_five_rails;
const
phrase : String = 'EIEXMSMESAORIWSCE';
expect : String = 'EXERCISMISAWESOME';
begin
TapAssertTrue(Self, 'decode with five rails', expect, RailFenceCipher.decode(phrase, 5));
end;

// 93e1ecf4-fac9-45d9-9cd2-591f47d3b8d3
procedure RailFenceCipherTest.decode_with_six_rails;
const
phrase : String = '133714114238148966225439541018335470986172518171757571896261';
expect : String = '112358132134558914423337761098715972584418167651094617711286';
begin
TapAssertTrue(Self, 'decode with six rails', expect, RailFenceCipher.decode(phrase, 6));
end;

initialization
RegisterTest(RailFenceCipherTest);

end.
Loading