From 15c7a2787cf2fa94c14f3ad1e4ae37a83b3a44ed Mon Sep 17 00:00:00 2001 From: derek mcquay Date: Fri, 12 Feb 2016 10:53:42 -0800 Subject: [PATCH] added detransposeSplit and tests --- psyfer/transposition.go | 33 +++++++++++++++++++ psyfer/transposition_test.go | 63 ++++++++++++++++++++++++++++++------ 2 files changed, 86 insertions(+), 10 deletions(-) diff --git a/psyfer/transposition.go b/psyfer/transposition.go index 4b097dd..570020e 100644 --- a/psyfer/transposition.go +++ b/psyfer/transposition.go @@ -43,3 +43,36 @@ func TransposeSplit(input string) string { } return split } + +func DeTransposeRailFence(input string) string { + rf := "" + for i := 0; i < len(input); i += 2 { + rf += string(input[i]) + } + for i := 1; i < len(input); i += 2 { + rf += string(input[i]) + } + return rf +} + +func DeTransposeSplit(input string) string { + desplit := "" + if len(input)%2 == 0 { + for i := 0; i < len(input); i += 2 { + desplit += string(input[i]) + } + for i := 1; i < len(input); i += 2 { + desplit += string(input[i]) + } + } else { + for i := 0; i < len(input)-2; i += 2 { + desplit += string(input[i]) + } + for i := 1; i < len(input)-2; i += 2 { + desplit += string(input[i]) + } + // fmt.Println(desplit) + desplit += string(input[len(input)-2]) + string(input[len(input)-1]) + } + return desplit +} diff --git a/psyfer/transposition_test.go b/psyfer/transposition_test.go index 3127ec9..dbd4574 100644 --- a/psyfer/transposition_test.go +++ b/psyfer/transposition_test.go @@ -8,7 +8,7 @@ func TestTransposeRailFence(t *testing.T) { actual := TransposeRailFence(input) if expected != actual { t.Errorf( - "failed TransposeRailFence:\n\texpected: % x\n\t actual: % x", + "failed TransposeRailFence:\n\texpected: % q\n\t actual: % q", expected, actual, ) @@ -18,7 +18,7 @@ func TestTransposeRailFence(t *testing.T) { actual = TransposeRailFence(input) if expected != actual { t.Errorf( - "failed TransposeRailFence:\n\texpected: % x\n\t actual: % x", + "failed TransposeRailFence:\n\texpected: % q\n\t actual: % q", expected, actual, ) @@ -28,7 +28,7 @@ func TestTransposeRailFence(t *testing.T) { actual = TransposeRailFence(input) if expected != actual { t.Errorf( - "failed TransposeRailFence:\n\texpected: % x\n\t actual: % x", + "failed TransposeRailFence:\n\texpected: % q\n\t actual: % q", expected, actual, ) @@ -38,7 +38,7 @@ func TestTransposeRailFence(t *testing.T) { actual = TransposeRailFence(input) if expected != actual { t.Errorf( - "failed TransposeRailFence:\n\texpected: % x\n\t actual: % x", + "failed TransposeRailFence:\n\texpected: % q\n\t actual: % q", expected, actual, ) @@ -51,7 +51,7 @@ func TestTransposeSplit(t *testing.T) { actual := TransposeSplit(input) if expected != actual { t.Errorf( - "failed TransposeSplit:\n\texpected: % x\n\t actual: % x", + "failed TransposeSplit:\n\texpected: % q\n\t actual: % q", expected, actual, ) @@ -61,7 +61,7 @@ func TestTransposeSplit(t *testing.T) { actual = TransposeSplit(input) if expected != actual { t.Errorf( - "failed TransposeSplit:\n\texpected: % x\n\t actual: % x", + "failed TransposeSplit:\n\texpected: % q\n\t actual: % q", expected, actual, ) @@ -71,7 +71,7 @@ func TestTransposeSplit(t *testing.T) { actual = TransposeSplit(input) if expected != actual { t.Errorf( - "failed TransposeSplit:\n\texpected: % x\n\t actual: % x", + "failed TransposeSplit:\n\texpected: % q\n\t actual: % q", expected, actual, ) @@ -81,7 +81,7 @@ func TestTransposeSplit(t *testing.T) { actual = TransposeSplit(input) if expected != actual { t.Errorf( - "failed TransposeSplit:\n\texpected: % x\n\t actual: % x", + "failed TransposeSplit:\n\texpected: % q\n\t actual: % q", expected, actual, ) @@ -91,7 +91,7 @@ func TestTransposeSplit(t *testing.T) { actual = TransposeSplit(input) if expected != actual { t.Errorf( - "failed TransposeSplit:\n\texpected: % x\n\t actual: % x", + "failed TransposeSplit:\n\texpected: % q\n\t actual: % q", expected, actual, ) @@ -103,9 +103,52 @@ func TestTransposeRandom(t *testing.T) { actual := TransposeSplit(input) if input == actual { t.Errorf( - "failed TransposeRandom:\n\texpected: something random\n\t actual: % x", + "failed TransposeRandom:\n\texpected: something random\n\t actual: % q", input, actual, ) } } + +func TestDeTransposeSplit(t *testing.T) { + expected := "helloworld" + input := "hweolrllod" + actual := DeTransposeSplit(input) + if expected != actual { + t.Errorf( + "failed DeTransposeSplit:\n\texpected: % q\n\t actual: % q", + expected, + actual, + ) + } + expected = "12" + input = "12" + actual = DeTransposeSplit(input) + if expected != actual { + t.Errorf( + "failed DeTransposeSplit:\n\texpected: % q\n\t actual: % q", + expected, + actual, + ) + } + expected = "12345" + input = "13245" + actual = DeTransposeSplit(input) + if expected != actual { + t.Errorf( + "failed DeTransposeSplit:\n\texpected: % q\n\t actual: % q", + expected, + actual, + ) + } + expected = "123" + input = "123" + actual = DeTransposeSplit(input) + if expected != actual { + t.Errorf( + "failed DeTransposeSplit:\n\texpected: % q\n\t actual: % q", + expected, + actual, + ) + } +}