mirror of
https://github.com/cubixle/codekata-golang.git
synced 2026-04-24 19:54:43 +01:00
20201225
This commit is contained in:
73
20201225/main.go
Normal file
73
20201225/main.go
Normal file
@@ -0,0 +1,73 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func main() {
|
||||
fmt.Println(strings.Trim("Bob hit a ball, the hit BALL flew far after it was hit.", ",."))
|
||||
}
|
||||
|
||||
func maxCountWord(s, ban string) string {
|
||||
s = strings.ToLower(s)
|
||||
sb := strings.Builder{}
|
||||
for _, cn := range s {
|
||||
c := byte(cn)
|
||||
if isAlphabet(c) || c == ' ' {
|
||||
sb.WriteByte(c)
|
||||
}
|
||||
}
|
||||
ws := strings.Split(sb.String(), " ")
|
||||
m := make(map[string]int)
|
||||
for _, w := range ws {
|
||||
if w == ban {
|
||||
continue
|
||||
}
|
||||
m[w]++
|
||||
}
|
||||
max := 0
|
||||
w := ""
|
||||
for k, v := range m {
|
||||
if v > max {
|
||||
max = v
|
||||
w = k
|
||||
}
|
||||
}
|
||||
return w
|
||||
}
|
||||
|
||||
func maxCountWord2(s, ban string) string {
|
||||
m := make(map[string]int)
|
||||
s = strings.ToLower(s)
|
||||
sb := strings.Builder{}
|
||||
for _, cn := range s {
|
||||
c := byte(cn)
|
||||
if isAlphabet(c) {
|
||||
sb.WriteByte(c)
|
||||
continue
|
||||
}
|
||||
if c == ' ' {
|
||||
word := sb.String()
|
||||
if word != ban {
|
||||
m[word]++
|
||||
}
|
||||
sb = strings.Builder{}
|
||||
continue
|
||||
}
|
||||
}
|
||||
max := 0
|
||||
w := ""
|
||||
for k, v := range m {
|
||||
if v > max {
|
||||
max = v
|
||||
w = k
|
||||
}
|
||||
}
|
||||
return w
|
||||
|
||||
}
|
||||
|
||||
func isAlphabet(c byte) bool {
|
||||
return c >= 'a' && c <= 'z'
|
||||
}
|
||||
59
20201225/main_test.go
Normal file
59
20201225/main_test.go
Normal file
@@ -0,0 +1,59 @@
|
||||
package main
|
||||
|
||||
import "testing"
|
||||
|
||||
func Test_maxCountWord(t *testing.T) {
|
||||
type args struct {
|
||||
s string
|
||||
ban string
|
||||
}
|
||||
tests := []struct {
|
||||
name string
|
||||
args args
|
||||
want string
|
||||
}{
|
||||
{
|
||||
"test1",
|
||||
args{
|
||||
s: "Bob hit a ball, the hit BALL flew far after it was hit.",
|
||||
ban: "hit",
|
||||
},
|
||||
"ball",
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
if got := maxCountWord(tt.args.s, tt.args.ban); got != tt.want {
|
||||
t.Errorf("maxCountWord() = %v, want %v", got, tt.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func Test_maxCountWord2(t *testing.T) {
|
||||
type args struct {
|
||||
s string
|
||||
ban string
|
||||
}
|
||||
tests := []struct {
|
||||
name string
|
||||
args args
|
||||
want string
|
||||
}{
|
||||
{
|
||||
"test1",
|
||||
args{
|
||||
s: "Bob hit a ball, the hit BALL flew far after it was hit.",
|
||||
ban: "hit",
|
||||
},
|
||||
"ball",
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
if got := maxCountWord2(tt.args.s, tt.args.ban); got != tt.want {
|
||||
t.Errorf("maxCountWord() = %v, want %v", got, tt.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user