Created
July 10, 2016 15:39
-
-
Save Ramshackle-Jamathon/2828c56ccfd2f81e31562c1b7784568e to your computer and use it in GitHub Desktop.
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
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