diff --git a/psyfer/transposition.go b/psyfer/transposition.go index 570020e..458887c 100644 --- a/psyfer/transposition.go +++ b/psyfer/transposition.go @@ -45,14 +45,21 @@ func TransposeSplit(input string) string { } func DeTransposeRailFence(input string) string { - rf := "" - for i := 0; i < len(input); i += 2 { - rf += string(input[i]) + derf := "" + length := len(input) + first := input[:length/2] + second := input[length/2:] + if length%2 == 0 { + for i, _ := range first { + derf += string(first[i]) + string(second[i]) + } + } else { + for i, _ := range first { + derf += string(first[i]) + string(second[i+1]) + } + derf += string(second[0]) } - for i := 1; i < len(input); i += 2 { - rf += string(input[i]) - } - return rf + return derf } func DeTransposeSplit(input string) string { @@ -71,7 +78,6 @@ func DeTransposeSplit(input string) string { 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 dbd4574..787568f 100644 --- a/psyfer/transposition_test.go +++ b/psyfer/transposition_test.go @@ -13,6 +13,16 @@ func TestTransposeRailFence(t *testing.T) { actual, ) } + input = "12345" + expected = "13524" + actual = TransposeRailFence(input) + if expected != actual { + t.Errorf( + "failed TransposeRailFence:\n\texpected: % q\n\t actual: % q", + expected, + actual, + ) + } input = "123456" expected = "135246" actual = TransposeRailFence(input) @@ -152,3 +162,36 @@ func TestDeTransposeSplit(t *testing.T) { ) } } + +func TestDeTransposeRailFence(t *testing.T) { + expected := "helloworld" + input := "hloolelwrd" + actual := DeTransposeRailFence(input) + if expected != actual { + t.Errorf( + "failed DeTransposeRailFence:\n\texpected: % q\n\t actual: % q", + expected, + actual, + ) + } + expected = "12345" + input = "13524" + actual = DeTransposeRailFence(input) + if expected != actual { + t.Errorf( + "failed DeTransposeRailFence:\n\texpected: % q\n\t actual: % q", + expected, + actual, + ) + } + expected = "123" + input = "132" + actual = DeTransposeRailFence(input) + if expected != actual { + t.Errorf( + "failed DeTransposeRailFence:\n\texpected: % q\n\t actual: % q", + expected, + actual, + ) + } +}