This commit is contained in:
VicRen
2020-11-20 09:50:04 +08:00
parent 681c9a12b8
commit 2cbe93b3a9
2 changed files with 142 additions and 0 deletions

38
20201120/main.go Normal file
View File

@@ -0,0 +1,38 @@
package main
func main() {
}
func turnOver(s string) string {
var letters []int32
m := make(map[int]int32)
for i, c := range s {
if isLetter(c) {
letters = append(letters, c)
} else {
m[i] = c
}
}
var ret []int32
for i := len(letters) - 1; i >= 0; i-- {
ret = append(ret, letters[i])
}
for k, v := range m {
ret = insert(ret, k, v)
}
return string(ret)
}
func isLetter(s int32) bool {
return (s >= 'a' && s <= 'z') || (s >= 'A' && s <= 'Z')
}
func insert(a []int32, index int, value int32) []int32 {
if len(a) == index { // nil or empty slice or after last element
return append(a, value)
}
a = append(a[:index+1], a[index:]...) // index < len(a)
a[index] = value
return a
}

104
20201120/main_test.go Normal file
View File

@@ -0,0 +1,104 @@
package main
import (
"reflect"
"testing"
)
func Test_turnOver(t *testing.T) {
type args struct {
s string
}
tests := []struct {
name string
args args
want string
}{
{
"example_1",
args{"ab-cd"},
"dc-ba",
},
{
"example_2",
args{"a-bC-dEf-ghIj"},
"j-Ih-gfE-dCba",
},
{
"example_3",
args{"Test1ng-Leet=code-Q!"},
"Qedo1ct-eeLg=ntse-T!",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := turnOver(tt.args.s); got != tt.want {
t.Errorf("turnOver() = %v, want %v", got, tt.want)
}
})
}
}
func Test_isLetter(t *testing.T) {
type args struct {
s int32
}
tests := []struct {
name string
args args
want bool
}{
{
"a",
args{'a'},
true,
},
{
"U",
args{'U'},
true,
},
{
"-",
args{'-'},
false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := isLetter(tt.args.s); got != tt.want {
t.Errorf("isLetter() = %v, want %v", got, tt.want)
}
})
}
}
func Test_insert(t *testing.T) {
type args struct {
a []int32
index int
value int32
}
tests := []struct {
name string
args args
want []int32
}{
{
"test_1",
args{
[]int32{1, 2, 3},
1,
32,
},
[]int32{1, 32, 2, 3},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := insert(tt.args.a, tt.args.index, tt.args.value); !reflect.DeepEqual(got, tt.want) {
t.Errorf("insert() = %v, want %v", got, tt.want)
}
})
}
}