Documentation
¶
Overview ¶
Package gocombinatorics contains routines for generating permutations, combinations, and cartesian products of a list of elements similar to what the itertools python module offers.
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Stream ¶
type Stream interface {
// Next populates values with the next tuple and returns true.
// If there are no more tuples, Next returns false and leaves values
// unchanged. Caller must pass in a slice big enough to hold a tuple.
Next(values []int) bool
// TupleSize returns the size of tuples this stream emits. Caller must
// pass a slice of at least this size to the Next method.
TupleSize() int
// Reset resets this stream to the state it had when it was first created.
// After calling Reset, Next will yield the first tuple.
Reset()
}
Stream represents a finite stream of tuples
func Cartesian ¶
Cartesian returns the cartesian product of (0, 1, 2, ..., s0-1), (0, 1, 2, ..., s1-1), (0, 1, 2, ..., s2-1) etc. For instance, Cartesian(3, 2, 4) yields (0,0,0), (0,0,1), (0,0,2), (0,0,3), (0,1,0), (0,1,1), (0,1,2), (0,1,3), (1,0,0), (1,0,1), (1,0,2), (1,0,3), (1,1,0), (1,1,1), (1,1,2), (1,1,3), (2,0,0), (2,0,1), (2,0,2), (2,0,3), (2,1,0), (2,1,1), (2,1,2), (2,1,3)
func Combinations
deprecated
Combinations yields all the ways you can pick k ints from 0 to n-1 inclusive where order does not matter. The returned Stream's Next method will yield k-tuples.
For instance, Combinations(4,2) yields (0,1), (0,2), (0,3), (1,2), (1,3), (2,3)
Deprecated: Use TCombinations instead.
func CombinationsWithReplacement
deprecated
CombinationsWithReplacement is like Combinations except that returned tuples may contain duplicates. For instance, CombinationsWithReplacement(4, 2) yields (0,0), (0,1), (0,2), (0,3), (1,1), (1,2), (1,3), (2,2), (2,3), (3,3)
Deprecated: Use TCombinationsWithReplacement instead.
func OpsPosits ¶
OpsPosits yields all the possible positions of k binary operators in a postfix expression as a sequence of k-tuples. A value of 0 means the operator comes after the first number; 1 means the operator comes after the second number etc. Each value in a tuple is the same or greater than the previous value. Moreover the 1st value in a tuple must be at least 1; the 2nd value in a tuple must be at least 2 etc. None of the values in a tuple can be greater than k.
For instance, OpsPosits(4) yeilds (1,2,3,4), (1,2,4,4), (1,3,3,4), (1,3,4,4), (1,4,4,4), (2,2,3,4) (2,2,4,4), (2,3,3,4), (2,3,4,4), (2,4,4,4), (3,3,3,4), (3,3,4,4) (3,4,4,4), (4,4,4,4)
func Permutations
deprecated
Permutations yields all the ways you can pick k ints from 0 to n-1 inclusive where order matters. The returned Stream's Next method will yield k-tuples.
For instance, Permutations(4,2) yields (0,1), (0,2), (0,3), (1,0), (1,2), (1,3), (2,0), (2,1), (2,3), (3,0), (3,1), (3,2)
Deprecated: Use TPermutations instead.
type TStream ¶ added in v1.1.0
type TStream[T any] struct { // contains filtered or unexported fields }
TStream is like Stream but it emits tuples of type T. The zero value emits no tuples. Copying a TStream is not supported and may lead to errors.
func TCombinations ¶ added in v1.1.0
TCombinations yields all the ways you can pick k items from the items slice without replacement where order does not matter.
Example ¶
package main
import (
"fmt"
"github.com/keep94/gocombinatorics"
)
func main() {
// Print out all the ways you can choose 2 marbles from an urn
// containing a red, green, yellow, and blue marble without replacement
// and where order doesn't matter.
stream := gocombinatorics.TCombinations(
[]string{"red", "green", "yellow", "blue"}, 2)
picked := make([]string, stream.TupleSize())
for stream.Next(picked) {
fmt.Println(picked)
}
}
Output: [red green] [red yellow] [red blue] [green yellow] [green blue] [yellow blue]
func TCombinationsWithReplacement ¶ added in v1.1.0
TCombinationsWithReplacement yields all the ways you can pick k items from the items slice with replacement where order does not matter.
func TPermutations ¶ added in v1.1.0
TPermutations yields all the ways you can pick k items from the items slice without replacement where order matters.
func TProduct ¶ added in v1.1.0
TProduct yields all the ways you can pick k items from the items slice with replacement where order matters.
func (*TStream[T]) Next ¶ added in v1.1.0
Next populates values with the next tuple and returns true. If there are no more tuples, Next returns false and leaves values unchanged. Caller must pass in a slice big enough to hold a tuple.