mirror of
https://github.com/cubixle/codekata-golang.git
synced 2026-04-24 21:24:46 +01:00
20201218
This commit is contained in:
33
20201218/main.go
Normal file
33
20201218/main.go
Normal 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
65
20201218/main_test.go
Normal 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)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user