Skip to content

Commit 0a6c014

Browse files
committed
added frontBack
1 parent 4e528a9 commit 0a6c014

File tree

2 files changed

+41
-0
lines changed

2 files changed

+41
-0
lines changed

warmup1/frontBack.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package warmup1
2+
3+
// https://codingbat.com/prob/p123384
4+
5+
func frontBack(str string) string {
6+
n := len(str)
7+
if n == 1 {
8+
return str
9+
}
10+
return str[n-1:n] + str[1:n-1] + str[0:1]
11+
}

warmup1/frontBack_test.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package warmup1
2+
3+
import (
4+
"strings"
5+
"testing"
6+
)
7+
8+
func TestSwap(t *testing.T) {
9+
actual := frontBack("back")
10+
expected := "kacb"
11+
if strings.Compare(expected, actual) != 0 {
12+
t.Fatalf("expected \"%s\" but actual is \"%s\"", expected, actual)
13+
}
14+
}
15+
16+
func TestOneChar(t *testing.T) {
17+
expected := frontBack("x")
18+
actual := "x"
19+
if strings.Compare(expected, actual) != 0 {
20+
t.Fatalf("expected \"%s\" but actual is \"%s\"", expected, actual)
21+
}
22+
}
23+
24+
func TestOddChars(t *testing.T) {
25+
actual := frontBack("abcde")
26+
expected := "ebcda"
27+
if strings.Compare(expected, actual) != 0 {
28+
t.Fatalf("expected \"%s\" but actual is \"%s\"", expected, actual)
29+
}
30+
}

0 commit comments

Comments
 (0)