Create a gist now

Instantly share code, notes, and snippets.

@johannesh /main.go Secret
Created Aug 10, 2016

Permutator for John
package main
import "fmt"
// import "math"
func main() {
source := [][]string{
[]string{"prefix"},
[]string{"b", "B"},
[]string{"o", "O", "0"},
[]string{"a", "b", "c"},
}
permutate(source)
}
func permutate(source [][]string) {
lengths := make([]int, len(source))
for i := 0; i < len(source); i++ {
lengths[i] = len(source[i])
}
var count = 1
for i := 0; i < len(lengths); i++ {
count *= lengths[i]
}
word := make([]int, len(lengths))
for i := 0; i < len(word); i++ {
word[i] = 0
}
for i := 0; i < count; i++ {
printWord(word, source)
addOneWithOverflow(word, lengths)
}
}
func printWord(word []int, source [][]string) {
for j := 0; j < len(word); j++ {
fmt.Print(source[j][word[j]])
}
fmt.Print("\n")
}
func addOneWithOverflow(nums []int, limits []int) {
maxindex := len(nums) - 1
var add = true
for i := maxindex; add && i >= 0; i-- {
val := nums[i]
newval := (val + 1) % limits[i]
nums[i] = newval
if val + 1 == newval {
break
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment