This commit is contained in:
VicRen
2020-12-03 15:30:04 +08:00
parent 410825768e
commit e16c9db6e6
4 changed files with 114 additions and 3 deletions

View File

@@ -14,7 +14,7 @@ func main() {
} }
func RomaToDigit(s string) int { func romaToDigit(s string) int {
l := len(s) l := len(s)
ret := 0 ret := 0
for i := 0; i < l; i++ { for i := 0; i < l; i++ {

View File

@@ -36,11 +36,21 @@ func TestRomaToDigit(t *testing.T) {
args{"MCMXCIV"}, args{"MCMXCIV"},
1994, 1994,
}, },
{
"test6",
args{"CMXCIX"},
999,
},
{
"test7",
args{"XLIX"},
49,
},
} }
for _, tt := range tests { for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) { t.Run(tt.name, func(t *testing.T) {
if got := RomaToDigit(tt.args.s); got != tt.want { if got := romaToDigit(tt.args.s); got != tt.want {
t.Errorf("RomaToDigit() = %v, want %v", got, tt.want) t.Errorf("romaToDigit() = %v, want %v", got, tt.want)
} }
}) })
} }

30
20201203/main.go Normal file
View File

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

71
20201203/main_test.go Normal file
View File

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