mirror of
https://github.com/cubixle/codekata-golang.git
synced 2026-04-24 23:04:42 +01:00
20201203
This commit is contained in:
@@ -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++ {
|
||||||
|
|||||||
@@ -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
30
20201203/main.go
Normal 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
71
20201203/main_test.go
Normal 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)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user