1

这是 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

4

2 回答 2

3

您不能比较expectedand actual!=因为它比较地图的字符串表示形式,所以它只会随机工作(如果值以相同的顺序打印)。

您要做的就是使用reflectpackageDeepEqual()方法来比较地图:

import "reflect"
// ...

if !reflect.DeepEqual(tt.output, WordCount(tt.input)) {
// ...

它将首先检查两个映射是否为nil,然后检查它们是否具有相同的长度,然后检查它们是否具有相同的 (key, value) 对集。

于 2014-02-13T05:28:46.987 回答
2

您不是在比较两个地图,而是在比较两个地图的 String() 输出。但是,当打印地图或 range()'d 时,内容是随机选择的,因此您无法将其与字符串进行比较。

您可以先比较长度,然后 range() 其中一个,并检查第一个映射中每个键的值是否存在并且等于第二个映射中相同键的值。

于 2014-02-13T05:21:05.987 回答