From 6bc2d8ea4879c0aa483c11002653128f1d2f30ac Mon Sep 17 00:00:00 2001 From: VicRen Date: Sat, 12 Dec 2020 22:01:17 +0800 Subject: [PATCH] 20201212 --- 20201212/main.go | 17 +++++++++++++++++ 20201212/main_test.go | 39 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 56 insertions(+) create mode 100644 20201212/main.go create mode 100644 20201212/main_test.go diff --git a/20201212/main.go b/20201212/main.go new file mode 100644 index 0000000..eb2759a --- /dev/null +++ b/20201212/main.go @@ -0,0 +1,17 @@ +package main + +func main() { + +} + +func compress(src []int) []int { + var ret []int + for i := 0; 2*i+1 < len(src); i++ { + freq := src[2*i] + val := src[2*i+1] + for j := 0; j < freq; j++ { + ret = append(ret, val) + } + } + return ret +} diff --git a/20201212/main_test.go b/20201212/main_test.go new file mode 100644 index 0000000..62aef0b --- /dev/null +++ b/20201212/main_test.go @@ -0,0 +1,39 @@ +package main + +import ( + "reflect" + "testing" +) + +func Test_compress(t *testing.T) { + type args struct { + src []int + } + tests := []struct { + name string + args args + want []int + }{ + { + "test1", + args{ + []int{1, 2, 3, 4}, + }, + []int{2, 4, 4, 4}, + }, + { + "test2", + args{ + []int{1, 1, 2, 3}, + }, + []int{1, 3, 3}, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if got := compress(tt.args.src); !reflect.DeepEqual(got, tt.want) { + t.Errorf("compress() = %v, want %v", got, tt.want) + } + }) + } +}