This commit is contained in:
VicRen
2020-12-28 09:05:03 +08:00
parent 116e0e4f66
commit f51292dc00
2 changed files with 111 additions and 0 deletions

37
20201228/main.go Normal file
View File

@@ -0,0 +1,37 @@
package main
import (
"strings"
)
func main() {
}
func transform(s string) []string {
return process(strings.Split(s, " "))
}
func process(src []string) []string {
maxL := 0
for _, s := range src {
if len(s) > maxL {
maxL = len(s)
}
}
mid := make([]string, maxL)
for i := 0; i < len(mid); i++ {
for _, s := range src {
if i < len(s) {
mid[i] += string(s[i])
} else {
mid[i] += " "
}
}
}
var ret []string
for _, s := range mid {
ret = append(ret, strings.TrimRight(s, " "))
}
return ret
}

74
20201228/main_test.go Normal file
View File

@@ -0,0 +1,74 @@
package main
import (
"reflect"
"testing"
)
func Test_process(t *testing.T) {
type args struct {
src []string
}
tests := []struct {
name string
args args
want []string
}{
{
"test1",
args{[]string{"HOW", "ARE", "YOU"}},
[]string{"HAY", "ORO", "WEU"},
},
{
"test2",
args{[]string{"TO", "BE", "OR", "NOT", "TO", "BE"}},
[]string{"TBONTB", "OEROOE", " T"},
},
{
"test3",
args{[]string{"CONTEST", "IS", "COMING"}},
[]string{"CIC", "OSO", "N M", "T I", "E N", "S G", "T"},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := process(tt.args.src); !reflect.DeepEqual(got, tt.want) {
t.Errorf("process() = %v, want %v", got, tt.want)
}
})
}
}
func Test_transform(t *testing.T) {
type args struct {
s string
}
tests := []struct {
name string
args args
want []string
}{
{
"test1",
args{"HOW ARE YOU"},
[]string{"HAY", "ORO", "WEU"},
},
{
"test2",
args{"TO BE OR NOT TO BE"},
[]string{"TBONTB", "OEROOE", " T"},
},
{
"test3",
args{"CONTEST IS COMING"},
[]string{"CIC", "OSO", "N M", "T I", "E N", "S G", "T"},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := transform(tt.args.s); !reflect.DeepEqual(got, tt.want) {
t.Errorf("transform() = %v, want %v", got, tt.want)
}
})
}
}