Created
July 14, 2017 06:33
-
-
Save tenfyzhong/1c842ac395acd3f974dd837d36b17151 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package combination | |
// 对list求所有的子序列 | |
// 比如:西藏区,西的拼音有:xi,藏的拼音有:zang,cang,区的拼音有:qu,ou | |
// 求出所有的子串:西,西藏,西区,西藏区,藏,藏区,区 | |
// 的所有拼音组合 | |
func subCombination(list [][]string) []string { | |
if len(list) == 0 { | |
return nil | |
} | |
results := list[0] | |
for _, words := range list[1:] { | |
tmpResult := make([]string, 0, len(results)*len(words)) | |
for _, result := range results { | |
for _, word := range words { | |
if len(word) == 0 { | |
continue | |
} | |
tmpResult = append(tmpResult, result+word) | |
} | |
} | |
results = append(results, tmpResult...) | |
} | |
return results | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package combination | |
import ( | |
"testing" | |
"github.com/stretchr/testify/assert" | |
) | |
func TestSubCombinationEmpty(t *testing.T) { | |
assert.Nil(t, subCombination(nil)) | |
assert.Nil(t, subCombination([][]string{})) | |
} | |
func TestSubCombinationXi(t *testing.T) { | |
expected := []string{"xi"} | |
actual := subCombination([][]string{[]string{"xi"}}) | |
assert.Equal(t, expected, actual) | |
} | |
func TestSubCombinationXiZang(t *testing.T) { | |
expected := []string{"xi", "xizang", "xicang"} | |
actual := subCombination([][]string{[]string{"xi"}, []string{"zang", "cang"}}) | |
assert.Equal(t, expected, actual) | |
} | |
func TestSubCombinationXiZangQu(t *testing.T) { | |
expected := []string{"xi", "xizang", "xicang", "xiqu", "xiou", "xizangqu", "xizangou", "xicangqu", "xicangou"} | |
actual := subCombination([][]string{[]string{"xi"}, []string{"zang", "cang"}, []string{"qu", "ou"}}) | |
assert.Equal(t, expected, actual) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment