Skip to content

Instantly share code, notes, and snippets.

@Ramshackle-Jamathon
Created July 10, 2016 15:39
Show Gist options
  • Save Ramshackle-Jamathon/2828c56ccfd2f81e31562c1b7784568e to your computer and use it in GitHub Desktop.
Save Ramshackle-Jamathon/2828c56ccfd2f81e31562c1b7784568e to your computer and use it in GitHub Desktop.
package main
import (
"fmt"
)
func main() {
for perm := range GeneratePermutations([]int{1,2,3,4,5,6,7}){
fmt.Println(perm)
}
}
func GeneratePermutations(data []int) <-chan []int {
c := make(chan []int)
go func(c chan []int) {
defer close(c)
permutate(c, data)
}(c)
return c
}
func permutate(c chan []int, inputs []int){
output := make([]int, len(inputs))
copy(output, inputs)
c <- output
size := len(inputs)
p := make([]int, size + 1)
for i := 0; i < size + 1; i++ {
p[i] = i;
}
for i := 1; i < size;{
p[i]--
j := 0
if i % 2 == 1 {
j = p[i]
}
tmp := inputs[j]
inputs[j] = inputs[i]
inputs[i] = tmp
output := make([]int, len(inputs))
copy(output, inputs)
c <- output
for i = 1; p[i] == 0; i++{
p[i] = i
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment