From e16c9db6e667c6dc4683eb88b2e030f113015482 Mon Sep 17 00:00:00 2001 From: VicRen Date: Thu, 3 Dec 2020 15:30:04 +0800 Subject: [PATCH] 20201203 --- 20201202/main.go | 2 +- 20201202/main_test.go | 14 +++++++-- 20201203/main.go | 30 ++++++++++++++++++ 20201203/main_test.go | 71 +++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 114 insertions(+), 3 deletions(-) create mode 100644 20201203/main.go create mode 100644 20201203/main_test.go diff --git a/20201202/main.go b/20201202/main.go index af90801..85db793 100644 --- a/20201202/main.go +++ b/20201202/main.go @@ -14,7 +14,7 @@ func main() { } -func RomaToDigit(s string) int { +func romaToDigit(s string) int { l := len(s) ret := 0 for i := 0; i < l; i++ { diff --git a/20201202/main_test.go b/20201202/main_test.go index 7114905..5bb021b 100644 --- a/20201202/main_test.go +++ b/20201202/main_test.go @@ -36,11 +36,21 @@ func TestRomaToDigit(t *testing.T) { args{"MCMXCIV"}, 1994, }, + { + "test6", + args{"CMXCIX"}, + 999, + }, + { + "test7", + args{"XLIX"}, + 49, + }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - if got := RomaToDigit(tt.args.s); got != tt.want { - t.Errorf("RomaToDigit() = %v, want %v", got, tt.want) + if got := romaToDigit(tt.args.s); got != tt.want { + t.Errorf("romaToDigit() = %v, want %v", got, tt.want) } }) } diff --git a/20201203/main.go b/20201203/main.go new file mode 100644 index 0000000..37a307d --- /dev/null +++ b/20201203/main.go @@ -0,0 +1,30 @@ +package main + +func main() { + +} + +func goodPairsCount(src []int) int { + count := 0 + l := len(src) + for i := 0; i < l; i++ { + for j := i + 1; j < l; j++ { + if src[i] == src[j] { + count++ + } + } + } + return count +} + +func goodPairsCount2(src []int) int { + m := make(map[int]int) + for _, n := range src { + m[n] += 1 + } + count := 0 + for _, v := range m { + count += v * (v - 1) / 2 + } + return count +} diff --git a/20201203/main_test.go b/20201203/main_test.go new file mode 100644 index 0000000..8614b6a --- /dev/null +++ b/20201203/main_test.go @@ -0,0 +1,71 @@ +package main + +import "testing" + +func Test_goodPairsCount(t *testing.T) { + type args struct { + src []int + } + tests := []struct { + name string + args args + want int + }{ + { + "test1", + args{[]int{1, 2, 3, 1, 1, 3}}, + 4, + }, + { + "test2", + args{[]int{1, 1, 1, 1}}, + 6, + }, + { + "test3", + args{[]int{1, 2, 3}}, + 0, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if got := goodPairsCount(tt.args.src); got != tt.want { + t.Errorf("goodPairsCount() = %v, want %v", got, tt.want) + } + }) + } +} + +func Test_goodPairsCount2(t *testing.T) { + type args struct { + src []int + } + tests := []struct { + name string + args args + want int + }{ + { + "test1", + args{[]int{1, 2, 3, 1, 1, 3}}, + 4, + }, + { + "test2", + args{[]int{1, 1, 1, 1}}, + 6, + }, + { + "test3", + args{[]int{1, 2, 3}}, + 0, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if got := goodPairsCount2(tt.args.src); got != tt.want { + t.Errorf("goodPairsCount() = %v, want %v", got, tt.want) + } + }) + } +}