Solution: Permutations
examples/permutations/permutations.go
package main import ( "fmt" ) func main() { results := permutations("a") fmt.Println(results) } // We assume they are unique func permutations(word string) []string { if word == "" { return []string{""} } perms := []string{} for i, rn := range word { rest := word[:i] + word[i+1:] //fmt.Println(rest) for _, result := range permutations(rest) { perms = append(perms, fmt.Sprintf("%c", rn)+result) } //perms = append(perms, fmt.Sprintf("%c\n", rn)) } return perms }
examples/permutations/permutations_test.go
package main import ( "fmt" "sort" "testing" ) func TestPermutations(t *testing.T) { cases := make(map[string][]string) cases["a"] = []string{"a"} cases["ab"] = []string{"ab", "ba"} cases["abc"] = []string{"abc", "acb", "bac", "bca", "cab", "cba"} cases["abcd"] = []string{ "dabc", "dacb", "dbac", "dbca", "dcab", "dcba", "adbc", "adcb", "bdac", "bdca", "cdab", "cdba", "abdc", "acdb", "badc", "bcda", "cadb", "cbda", "abcd", "acbd", "bacd", "bcad", "cabd", "cbad"} for inp, expected := range cases { actual := permutations(inp) if !compare(expected, actual) { t.Error(fmt.Sprintf("Expected '%v', Actual '%v'", expected, actual)) } } } func compare(a, b []string) bool { //fmt.Println(a) //fmt.Println(b) if len(a) != len(b) { return false } sort.Strings(a) sort.Strings(b) for i := 0; i < len(a); i++ { if a[i] != b[i] { return false } } return true }