This commit is contained in:
VicRen
2020-12-12 22:01:17 +08:00
parent e5fca8a7a9
commit 6bc2d8ea48
2 changed files with 56 additions and 0 deletions

17
20201212/main.go Normal file
View File

@@ -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
}

39
20201212/main_test.go Normal file
View File

@@ -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)
}
})
}
}