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 {
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

View File

@ -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,
)
}
}