Esempio n. 1
0
func lexv(alphabet string, length int) {
	var (
		a []uint8
	)

	selectors := make(chan string, 10)
	counts := make(chan int)

	max := int(math.Pow(float64(length+1), float64(len(alphabet))))
	for i := 0; i != MAXPROCS; i++ {
		go gen_selectors(alphabet, i*(max/MAXPROCS), (i+1)*(max/MAXPROCS), length, selectors, counts)
	}

	count, counts_rcvd, processed := 0, 0, 0
	res := make([]string, 0)

	for {
		select {
		case selector := <-selectors:
			a = make([]uint8, 0)

			for j := 0; j != len(selector); j++ {
				for k := 0; k != int(selector[j]-'0'); k++ {
					a = append(a, alphabet[len(selector)-j-1])
				}
			}

			sort.Sort(permutable.PermChars(a))

			for {
				res = append(res, string(a))
				if !permutable.NextPermutation(permutable.PermChars(a)) {
					break
				}
			}

			processed++
		case c := <-counts:
			count += c
			counts_rcvd++

			if counts_rcvd == MAXPROCS {
				close(counts)
				counts = nil
			}
		}

		if counts_rcvd == MAXPROCS && count == processed {
			close(selectors)
			break
		}
	}

	ls := LexSort{alphabet, res}
	sort.Sort(ls)
	ls.Print()
}
Esempio n. 2
0
func sign(n int) {
	fmt.Println(factorial(n) * (1 << uint8(n)))

	a := make([]int, n)
	for k := range a {
		a[k] = k + 1
	}

	signPerm(a)
	for permutable.NextPermutation(permutable.PermInts(a)) {
		signPerm(a)
	}
}
Esempio n. 3
0
func Perm() {
	n := 0

	if _, err := fmt.Scanf("%d", &n); err != nil {
		log.Fatal(err)
	}

	fmt.Println(factorial(n))

	a := make([]int, n)
	for k := range a {
		a[k] = k + 1
	}

	printPerm(a)
	for permutable.NextPermutation(permutable.PermInts(a)) {
		printPerm(a)
	}
}