Skip to content

Instantly share code, notes, and snippets.

@tenfyzhong
Last active July 13, 2017 04:15
Show Gist options
  • Save tenfyzhong/604345088850214cc34620b8945bc3ce to your computer and use it in GitHub Desktop.
Save tenfyzhong/604345088850214cc34620b8945bc3ce to your computer and use it in GitHub Desktop.
func mergeBetweenWord(list [][]string) []string {
if len(list) == 0 {
return []string{}
}
levelResult := [][]string{}
levelResult = append(levelResult, []string{""}) // 塞一个空,下面就不用进行判断是否空了
count := 0
for i, word := range list {
newLevel := make([]string, 0, len(levelResult[i])*len(word))
for _, upper := range levelResult[i] {
for _, pinyin := range word {
newLevel = append(newLevel, upper+pinyin)
count++
}
}
levelResult = append(levelResult, newLevel)
}
result := make([]string, 0, count)
// 从1开始,因为0是上面主动塞的空
for _, v := range levelResult[1:] {
result = append(result, v...)
}
return result
}
func subFullPinyinList(list [][]string) []string {
result := make([]string, 0, len(list)*len(list)/1)
for i := 0; i < len(list); i++ {
r := mergeBetweenWord(list[i:])
result = append(result, r...)
}
return result
}
package search
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestEmptyList(t *testing.T) {
result := subFullPinyinList([][]string{})
assert.Equal(t, []string{}, result)
}
func Test1Word(t *testing.T) {
xi := []string{"xi"}
param := make([][]string, 0, 1)
param = append(param, xi)
result := subFullPinyinList(param)
assert.Equal(t, []string{"xi"}, result)
}
func TestZhongGuo(t *testing.T) {
zhong := []string{"zhong"}
guo := []string{"guo"}
param := make([][]string, 0, 2)
param = append(param, zhong)
param = append(param, guo)
result := subFullPinyinList(param)
assert.Equal(t, []string{"zhong", "zhongguo", "guo"}, result)
}
func TestXiZang(t *testing.T) {
xi := []string{"xi"}
zang := []string{"zang", "cang"}
param := make([][]string, 0, 2)
param = append(param, xi)
param = append(param, zang)
result := subFullPinyinList(param)
assert.Equal(t, []string{"xi", "xizang", "xicang", "zang", "cang"}, result)
}
func TestXiZangQu(t *testing.T) {
xi := []string{"xi"}
zang := []string{"zang", "cang"}
qu := []string{"qu", "ou"}
param := make([][]string, 0, 15)
param = append(param, xi)
param = append(param, zang)
param = append(param, qu)
result := subFullPinyinList(param)
expected := []string{"xi", "xizang", "xicang", "xizangqu", "xizangou",
"xicangqu", "xicangou", "zang", "cang", "zangqu", "zangou",
"cangqu", "cangou", "qu", "ou"}
assert.Equal(t, expected, result)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment