diff --git a/20201231/main.go b/20201231/main.go new file mode 100644 index 0000000..99f877c --- /dev/null +++ b/20201231/main.go @@ -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() +} diff --git a/20201231/main_test.go b/20201231/main_test.go new file mode 100644 index 0000000..5b07949 --- /dev/null +++ b/20201231/main_test.go @@ -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) + } + }) + } +}