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

# cezar's solution

## to Rail Fence Cipher in the Go Track

Published at Jan 15 2021 · 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.

## Coding the solution

Look for a stub file having the name rail_fence_cipher.go and place your solution code in that file.

## Running the tests

To run the tests run the command `go test` from within the exercise directory.

If the test suite contains benchmarks, you can run these with the `--bench` and `--benchmem` flags:

``````go test -v --bench . --benchmem
``````

Keep in mind that each reviewer will run benchmarks on a different machine, with different specs, so the results from these benchmark tests may vary.

## Further information

For more detailed information about the Go track, including how to get help if you're having trouble, please visit the exercism.io Go language page.

## Submitting Incomplete Solutions

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

### cases_test.go

``````package railfence

// Source: exercism/problem-specifications
// Commit: 88db37b rail-fence-cipher: apply "input" policy
// Problem Specifications Version: 1.1.0

type testCase struct {
description string
message     string
rails       int
expected    string
}

// encode
var encodeTests = []testCase{

{"encode with two rails",
"XOXOXOXOXOXOXOXOXO",
2,
"XXXXXXXXXOOOOOOOOO"},

{"encode with three rails",
"WEAREDISCOVEREDFLEEATONCE",
3,
"WECRLTEERDSOEEFEAOCAIVDEN"},

{"encode with ending in the middle",
"EXERCISES",
4,
"ESXIEECSR"},
}

// decode
var decodeTests = []testCase{

{"decode with three rails",
"TEITELHDVLSNHDTISEIIEA",
3,
"THEDEVILISINTHEDETAILS"},

{"decode with five rails",
"EIEXMSMESAORIWSCE",
5,
"EXERCISMISAWESOME"},

{"decode with six rails",
"133714114238148966225439541018335470986172518171757571896261",
6,
"112358132134558914423337761098715972584418167651094617711286"},
}``````

### rail_fence_cipher_test.go

``````package railfence

import "testing"

func testCases(op func(string, int) string, cases []testCase, t *testing.T) {
for _, tc := range cases {
if actual := op(tc.message, tc.rails); actual != tc.expected {
t.Fatalf("FAIL: %s\nExpected: %q\nActual: %q", tc.description, tc.expected, actual)
}
t.Logf("PASS: %s", tc.description)
}
}

func TestEncode(t *testing.T) { testCases(Encode, encodeTests, t) }
func TestDecode(t *testing.T) { testCases(Decode, decodeTests, t) }``````
``````/*
This package is used to 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.
*/
package railfence

// Function used to create a Frame
func CreateFrame(collength int, rowlength int) [][]rune {
var a int = 0
var b int = 1
matrix := make([][]rune, rowlength)

// create base
for i := 0; i < rowlength; i++ {
matrix[i] = make([]rune, collength)
for j := 0; j < collength; j++ {
matrix[i][j] = '.'
}
}

//put a mark on leter places
for k := 0; k < collength; k++ {

matrix[a][k] = '?'

if a == 0 {
b = 1
}
if a == (rowlength - 1) {
b = -1
}
a = a + b
}

return matrix
}

// Function used to fill in a Frame horizontally
func WriteFrameH(base [][]rune, instr string) {

var k int = 0
var r int = len(base)
var c int = len(base[0])

// write
for i := 0; i < r; i++ {
for j := 0; j < c; j++ {
if base[i][j] == '?' {
base[i][j] = rune(instr[k])
k++
}
}
}
}

// Function used to read horizontally from a Frame

var outstr []rune
var r int = len(base)
var c int = len(base[0])

for i := 0; i < r; i++ {
for j := 0; j < c; j++ {
if base[i][j] != '.' {
outstr = append(outstr, base[i][j])
}
}
}

return string(outstr)
}

// Function used to fill in a Frame vertically
func WriteFrameV(base [][]rune, instr string) {

var k int = 0
var r int = len(base)
var c int = len(base[0])

// write
for i := 0; i < c; i++ {

for j := 0; j < r; j++ {
if base[j][i] == '?' {
base[j][i] = rune(instr[k])
k++
}
}

}
}

// Function used to read vertically from a Frame

var outstr []rune
var r int = len(base)
var c int = len(base[0])

for i := 0; i < c; i++ {
for j := 0; j < r; j++ {
if base[j][i] != '.' {
outstr = append(outstr, base[j][i])
}
}
}

return string(outstr)
}

// Function used for Encoding a string
func Encode(strin string, rails int) string {

f1 := CreateFrame(len(strin), rails)

WriteFrameV(f1, strin)

return s1
}

// Function used for Decoding a string
func Decode(strin string, rails int) string {

f2 := CreateFrame(len(strin), rails)

WriteFrameH(f2, strin)

return s2
}``````