From 6ac236008b0d686b20c57749a337eb85cea25630 Mon Sep 17 00:00:00 2001 From: VicRen Date: Mon, 23 Nov 2020 09:06:47 +0800 Subject: [PATCH] 20201123 --- 20201123/main.go | 35 ++++++++++++++ 20201123/main_test.go | 109 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 144 insertions(+) create mode 100644 20201123/main.go create mode 100644 20201123/main_test.go diff --git a/20201123/main.go b/20201123/main.go new file mode 100644 index 0000000..4a692db --- /dev/null +++ b/20201123/main.go @@ -0,0 +1,35 @@ +package main + +import ( + "math/big" + "strconv" +) + +func main() { + +} + +func solution(k int, a []int) []int { + ks, _ := (&big.Int{}).SetString(strconv.Itoa(k), 0) + s := (&big.Int{}).Add(mergeSlice(a), ks).String() + return stringToSlice(s) +} + +func mergeSlice(n []int) *big.Int { + var s string + for _, x := range n { + sn := strconv.Itoa(x) + s += sn + } + ret, _ := (&big.Int{}).SetString(s, 0) + return ret +} + +func stringToSlice(s string) []int { + var ret []int + for _, sn := range s { + in, _ := strconv.Atoi(string(sn)) + ret = append(ret, in) + } + return ret +} diff --git a/20201123/main_test.go b/20201123/main_test.go new file mode 100644 index 0000000..6b82475 --- /dev/null +++ b/20201123/main_test.go @@ -0,0 +1,109 @@ +package main + +import ( + "reflect" + "testing" +) + +//func Test_mergeSlice(t *testing.T) { +// type args struct { +// n []int +// } +// tests := []struct { +// name string +// args args +// want int +// }{ +// { +// "example_1", +// args{[]int{1, 2, 0, 0}}, +// 1200, +// }, +// { +// "example_2", +// args{[]int{2, 7, 4}}, +// 274, +// }, +// { +// "example_3", +// args{[]int{2, 1, 5}}, +// 215, +// }, +// } +// for _, tt := range tests { +// t.Run(tt.name, func(t *testing.T) { +// if got := mergeSlice(tt.args.n); got != tt.want { +// t.Errorf("mergeSlice() = %v, want %v", got, tt.want) +// } +// }) +// } +//} + +func Test_stringToSlice(t *testing.T) { + type args struct { + s string + } + tests := []struct { + name string + args args + want []int + }{ + { + "example_1", + args{"1234"}, + []int{1, 2, 3, 4}, + }, + { + "example_2", + args{"455"}, + []int{4, 5, 5}, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if got := stringToSlice(tt.args.s); !reflect.DeepEqual(got, tt.want) { + t.Errorf("stringToSlice() = %v, want %v", got, tt.want) + } + }) + } +} + +func Test_solution(t *testing.T) { + type args struct { + k int + a []int + } + tests := []struct { + name string + args args + want []int + }{ + { + "example_1", + args{34, []int{1, 2, 0, 0}}, + []int{1, 2, 3, 4}, + }, + { + "example_2", + args{181, []int{2, 7, 4}}, + []int{4, 5, 5}, + }, + { + "example_3", + args{806, []int{2, 1, 5}}, + []int{1, 0, 2, 1}, + }, + { + "example_4", + args{1, []int{9, 9, 9, 9, 9, 9, 9, 9, 9, 9}}, + []int{1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if got := solution(tt.args.k, tt.args.a); !reflect.DeepEqual(got, tt.want) { + t.Errorf("solution() = %v, want %v", got, tt.want) + } + }) + } +}