This commit is contained in:
VicRen
2020-12-31 10:55:49 +08:00
parent bef6df88ed
commit 8b26b52149
2 changed files with 141 additions and 0 deletions

44
20201231/main.go Normal file
View File

@@ -0,0 +1,44 @@
package main
import (
"strconv"
"strings"
)
func main() {
}
func process(n int) string {
ret := ""
for i := 1; i <= n; i++ {
ret = say(ret)
}
return ret
}
func say(num string) string {
if num == "" {
return "1"
}
var nums []string
var n []uint8
last := num[0]
for i := 0; i < len(num); i++ {
if last == num[i] {
n = append(n, num[i])
} else {
nums = append(nums, string(n))
last = num[i]
n = nil
n = append(n, num[i])
}
}
nums = append(nums, string(n))
ret := strings.Builder{}
for _, s := range nums {
ret.WriteString(strconv.Itoa(len(s)))
ret.WriteByte(s[0])
}
return ret.String()
}

97
20201231/main_test.go Normal file
View File

@@ -0,0 +1,97 @@
package main
import "testing"
func Test_say(t *testing.T) {
type args struct {
num string
}
tests := []struct {
name string
args args
want string
}{
{
"test1",
args{
num: "",
},
"1",
},
{
"test2",
args{
num: "1",
},
"11",
},
{
"test3",
args{
num: "3322251",
},
"23321511",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := say(tt.args.num); got != tt.want {
t.Errorf("say() = %v, want %v", got, tt.want)
}
})
}
}
func Test_process(t *testing.T) {
type args struct {
n int
}
tests := []struct {
name string
args args
want string
}{
{
"test1",
args{
1,
},
"1",
},
{
"test2",
args{
2,
},
"11",
},
{
"test3",
args{
3,
},
"21",
},
{
"test4",
args{
4,
},
"1211",
},
{
"test4",
args{
5,
},
"111221",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := process(tt.args.n); got != tt.want {
t.Errorf("process() = %v, want %v", got, tt.want)
}
})
}
}