From d583fadd1d679d799631d34e0fe5022e7b9ff454 Mon Sep 17 00:00:00 2001 From: VicRen Date: Fri, 18 Dec 2020 08:51:54 +0800 Subject: [PATCH] 20201218 --- 20201218/main.go | 33 ++++++++++++++++++++++ 20201218/main_test.go | 65 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 98 insertions(+) create mode 100644 20201218/main.go create mode 100644 20201218/main_test.go diff --git a/20201218/main.go b/20201218/main.go new file mode 100644 index 0000000..8f92dbf --- /dev/null +++ b/20201218/main.go @@ -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 +} diff --git a/20201218/main_test.go b/20201218/main_test.go new file mode 100644 index 0000000..8dc27b9 --- /dev/null +++ b/20201218/main_test.go @@ -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) + } + }) + } +}