code kata 230809

This commit is contained in:
VicRen
2023-08-09 10:43:02 +08:00
parent 02e1a8ad1a
commit 800a0380b0
4 changed files with 262 additions and 0 deletions

View File

@@ -0,0 +1,32 @@
package main
import "testing"
func ReverseString(s string) string {
ret := []rune(s)
for l, r := 0, len(ret)-1; l < r; l, r = l+1, r-1 {
ret[l], ret[r] = ret[r], ret[l]
}
return string(ret)
}
func TestReverseString(t *testing.T) {
tt := []struct {
name string
input string
want string
}{
{"test", "test", "tset"},
{"testing", "testing", "gnitset"},
{"i am testing", "i am testing", "gnitset ma i"},
}
for _, tc := range tt {
t.Run(tc.name, func(t *testing.T) {
got := ReverseString(tc.input)
if got != tc.want {
t.Errorf("ReverseString(%s) = %s, want %s\n", tc.input, got, tc.want)
}
})
}
}