func initPermutation() error { a := []int{0, 1, 2, 3, 4, 5, 6} p, err := permutation.NewPerm(a, nil) if err != nil { return err } result := p.NextN(p.Left()) permutations = result.([][]int) log.Infof("Generated %d permutations", len(permutations)) return nil }
// Creates permutations of the persons involved, returning the maximum // happiness of any given permutation. func calculateMaxHappiness(persons []string, happinessMap map[relation]int) int { perm, err := permutation.NewPerm(persons, nil) if err != nil { panic(err) } maxHappiness := 0 for e, err := perm.Next(); err == nil; e, err = perm.Next() { current := e.([]string) happiness := calculateHappiness(current, happinessMap) if happiness > maxHappiness { maxHappiness = happiness } } return maxHappiness }
func find(originKey string) []string { arr := []string{"00", "01", "02", "03", "04", "05", "06", "07", "08"} p, err := permutation.NewPerm(arr, nil) //generate a Permutator util.CheckErr(err) for i, err := p.Next(); err == nil; i, err = p.Next() { vals := i.([]string) for i := 4; i <= len(arr); i++ { info := strings.Join(vals[0:i], "") //fmt.Println(info) tmpKey := sum(info) //fmt.Println(vals, tmpKey) if tmpKey == originKey { return vals[0:i] } } } return nil }
func main() { digits := []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 'x'} permutation, err := permutation.NewPerm(digits, nil) if err != nil { fmt.Println(err) return } for s, err := permutation.Next(); err == nil; s, err = permutation.Next() { permutatedDigits := s.([]int) a, b := 0, 0 beforeMark := true // optimization: permutation not possible since denominator has to be 4 digits long if permutatedDigits[4] != 'x' { continue } for i := 0; i < 10; i++ { if i == 4 { beforeMark = false } else { if beforeMark { a = a*10 + permutatedDigits[i] } else { b = b*10 + permutatedDigits[i] } } } if a > 0 && b > 0 && float32(b)/float32(a) == 3 { fmt.Println(a, b) } } }