added DeTransposeRailFence and tests

This commit is contained in:
Derek McQuay 2016-02-12 11:08:29 -08:00
parent 15c7a2787c
commit cc0649e38a
2 changed files with 57 additions and 8 deletions

View File

@ -45,14 +45,21 @@ func TransposeSplit(input string) string {
} }
func DeTransposeRailFence(input string) string { func DeTransposeRailFence(input string) string {
rf := "" derf := ""
for i := 0; i < len(input); i += 2 { length := len(input)
rf += string(input[i]) first := input[:length/2]
second := input[length/2:]
if length%2 == 0 {
for i, _ := range first {
derf += string(first[i]) + string(second[i])
} }
for i := 1; i < len(input); i += 2 { } else {
rf += string(input[i]) for i, _ := range first {
derf += string(first[i]) + string(second[i+1])
} }
return rf derf += string(second[0])
}
return derf
} }
func DeTransposeSplit(input string) string { func DeTransposeSplit(input string) string {
@ -71,7 +78,6 @@ func DeTransposeSplit(input string) string {
for i := 1; i < len(input)-2; i += 2 { for i := 1; i < len(input)-2; i += 2 {
desplit += string(input[i]) desplit += string(input[i])
} }
// fmt.Println(desplit)
desplit += string(input[len(input)-2]) + string(input[len(input)-1]) desplit += string(input[len(input)-2]) + string(input[len(input)-1])
} }
return desplit return desplit

View File

@ -13,6 +13,16 @@ func TestTransposeRailFence(t *testing.T) {
actual, 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" input = "123456"
expected = "135246" expected = "135246"
actual = TransposeRailFence(input) 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,
)
}
}