mirror of
https://github.com/cubixle/codekata-golang.git
synced 2026-04-24 21:24:46 +01:00
20201231
This commit is contained in:
44
20201231/main.go
Normal file
44
20201231/main.go
Normal 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
97
20201231/main_test.go
Normal 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)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user