Skip to content

Instantly share code, notes, and snippets.

@nicerobot
Created November 17, 2019 20:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nicerobot/492e11f325701d96472f3afe42264bc2 to your computer and use it in GitHub Desktop.
Save nicerobot/492e11f325701d96472f3afe42264bc2 to your computer and use it in GitHub Desktop.
Go string-permutation generator (not ideal since it doesn't nicely handle the channel and goroutine)
func PermutationGenerator(in string) chan string {
r := make(chan string, 0)
var perm func([]rune, int)
perm = func(a []rune, i int) {
if i > len(a) {
r <- string(a)
return
}
perm(a, i+1)
for j := i + 1; j < len(a); j++ {
a[i], a[j] = a[j], a[i]
perm(a, i+1)
a[i], a[j] = a[j], a[i]
}
if i == 0 {
close(r)
}
}
go perm([]rune(in), 0)
return r
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment