Created
December 18, 2013 00:04
-
-
Save whyrusleeping/8015116 to your computer and use it in GitHub Desktop.
Get all permutations of a set of strings
This file contains hidden or 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
| func permute(opts []string) []string { | |
| return _permute("", opts) | |
| } | |
| func _permute(s string, opts []string) []string { | |
| if len(opts) == 0 { | |
| return []string{s} | |
| } | |
| ret := make([]string, 0, 16) | |
| olist := make([]string, 0, len(opts)) | |
| for i := 0; i < len(opts); i++ { | |
| a := s + opts[i] | |
| olist = olist[:0] | |
| for j := 0; j < len(opts); j++ { | |
| if j != i { | |
| olist = append(olist, opts[j]) | |
| } | |
| } | |
| ret = append(ret, _permute(a, olist)...) | |
| } | |
| return ret | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment