Created
November 17, 2019 20:59
-
-
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)
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
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