code kata 230808

This commit is contained in:
VicRen
2023-08-08 22:08:51 +08:00
parent 5f7b76c60a
commit 02e1a8ad1a
7 changed files with 471 additions and 2 deletions
+12
View File
@@ -11,6 +11,14 @@ func ReverseString(s string) string {
return string(tmp)
}
func ReverseString2(s string) string {
tmp := []rune(s)
for f, t := 0, len(tmp)-1; t > f; f, t = f+1, t-1 {
tmp[f], tmp[t] = tmp[t], tmp[f]
}
return string(tmp)
}
func TestReverseString(t *testing.T) {
tt := []struct {
name string
@@ -28,6 +36,10 @@ func TestReverseString(t *testing.T) {
if got != tc.want {
t.Errorf("ReverseString(%s) = %s, want %s", tc.input, got, tc.want)
}
got = ReverseString2(tc.input)
if got != tc.want {
t.Errorf("ReverseString(%s) = %s, want %s", tc.input, got, tc.want)
}
})
}
}