这是 Go 中的字数统计功能
package wc
import (
"regexp"
"strings"
)
type Histogram map[string]int
func WordCount(input string) Histogram {
histogram := make(map[string]int)
re := regexp.MustCompile("[^a-zA-Z0-9 ]*")
input = re.ReplaceAllString(input, "")
for _, word := range strings.Split(input, " ") {
if word == "" {
continue
}
histogram[strings.ToLower(word)]++
}
return histogram
}
此代码不确定地通过或失败测试。有时由于预期地图和实际地图不匹配而失败。但是,两者的内容完全相同。我认为地图比较存在一些问题。我不知道我该如何解决。请有人帮助我!
这是测试套件代码
package wc
import (
"fmt"
"testing"
)
var testCases = []struct {
description string
input string
output Histogram
}{
{
description: "a single word",
input: "word",
output: Histogram{"word": 1},
},
{
description: "one of each",
input: "one of each",
output: Histogram{"one": 1, "of": 1, "each": 1},
},
{
description: "multiple occurrences",
input: "one fish two fish red fish blue fish",
output: Histogram{"one": 1, "fish": 4, "two": 1, "red": 1, "blue": 1},
},
{
description: "ignore punctuation",
input: "car : carpet as java : javascript!!&@$%^&",
output: Histogram{"car": 1, "carpet": 1, "as": 1, "java": 1, "javascript": 1},
},
{
description: "including numbers",
input: "testing, 1, 2 testing",
output: Histogram{"testing": 2, "1": 1, "2": 1},
},
{
description: "normalises case",
input: "go Go GO",
output: Histogram{"go": 3},
},
}
func TestWordCount(t *testing.T) {
for _, tt := range testCases {
expected := fmt.Sprintf("%v", tt.output)
actual := fmt.Sprintf("%v", WordCount(tt.input))
if expected != actual {
t.Fatalf("%s\n\tExpected: %v\n\tGot: %v", tt.description, expected, actual)
} else {
t.Logf("PASS: %s - WordCount(%s)", tt.description, tt.input)
}
}
}
以下是失败情况的示例:
1.
Expected: map[two:1 red:1 blue:1 one:1 fish:4]
Got: map[one:1 fish:4 two:1 red:1 blue:1]
2.
Expected: map[one:1 fish:4 two:1 red:1 blue:1]
Got: map[red:1 blue:1 one:1 fish:4 two:1]
3.
Expected: map[java:1 javascript:1 car:1 carpet:1 as:1]
Got: map[javascript:1 car:1 carpet:1 as:1 java:1]
...
更多信息在这里:
http ://exercism.io/submissions/cf94f4732fd97335be2e755f