This commit is contained in:
VicRen
2020-12-18 08:51:54 +08:00
parent e84dda0dee
commit d583fadd1d
2 changed files with 98 additions and 0 deletions

33
20201218/main.go Normal file
View File

@@ -0,0 +1,33 @@
package main
func main() {
}
func calculate(src []string) int {
var cw string
var max int
for i := 0; i < len(src)-1; i++ {
cw = src[i]
cl := len(cw)
for j := i + 1; j < len(src); j++ {
cw2 := src[j]
v := cl * len(cw2)
if !hasSameChar(cw, cw2) && (v > max) {
max = v
}
}
}
return max
}
func hasSameChar(s1, s2 string) bool {
for _, v := range s1 {
for _, v2 := range s2 {
if v == v2 {
return true
}
}
}
return false
}

65
20201218/main_test.go Normal file
View File

@@ -0,0 +1,65 @@
package main
import "testing"
func Test_calculate(t *testing.T) {
type args struct {
src []string
}
tests := []struct {
name string
args args
want int
}{
{
"test1",
args{src: []string{"abcw", "baz", "foo", "bar", "xtfn", "abcdef"}},
16,
},
{
"test2",
args{src: []string{"a", "ab", "abc", "d", "cd", "bcd", "abcd"}},
4,
},
{
"test3",
args{src: []string{"a", "aa", "aaa", "aaaa"}},
0,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := calculate(tt.args.src); got != tt.want {
t.Errorf("calculate() = %v, want %v", got, tt.want)
}
})
}
}
func Test_hasSameChar(t *testing.T) {
type args struct {
s1 string
s2 string
}
tests := []struct {
name string
args args
want bool
}{
{
"test1",
args{
s1: "abc",
s2: "ade",
},
true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := hasSameChar(tt.args.s1, tt.args.s2); got != tt.want {
t.Errorf("hasSameChar() = %v, want %v", got, tt.want)
}
})
}
}