Created
May 11, 2020 19:05
-
-
Save drewstaylor/9946d797302e0dbe0c037c72eb29ff9e to your computer and use it in GitHub Desktop.
GoLang permutations calculator
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
// Permutation generator in Golang (array of strings) | |
package main | |
import ( | |
"fmt" | |
"strings" | |
) | |
func nextPerm(p []int) { | |
for i := len(p) - 1; i >= 0; i-- { | |
if i == 0 || p[i] < len(p)-i-1 { | |
p[i]++ | |
return | |
} | |
p[i] = 0 | |
} | |
} | |
func getPerm(orig, p []int) []int { | |
result := append([]int{}, orig...) | |
for i, v := range p { | |
result[i], result[i+v] = result[i+v], result[i] | |
} | |
return result | |
} | |
func main() { | |
// Init | |
// Define array size and keys | |
keys := []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12} | |
// Define seed words to permutate | |
seed_values := []string{"word1", "word2", "word3", "word4", "word5", "word6", "word7", "word8", "word9", "word10", "word11", "word12"} | |
// Worker | |
for p := make([]int, len(keys)); p[0] < len(p); nextPerm(p) { | |
_currentPermsMap := getPerm(keys, p) | |
var res []string | |
for i := range _currentPermsMap { | |
key_shift := (_currentPermsMap[i] - 1) | |
resulter := seed_values[key_shift] | |
res = append(res, resulter) | |
} | |
fmt.Println(strings.Join(res, " ")) | |
} | |
fmt.Println("Finished!") | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment