gocombinatorics

package module
v1.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 27, 2023 License: BSD-3-Clause Imports: 3 Imported by: 2

README

gocombinatorics

Provides iterators for permutations and the like

Using

import "github.com/keep94/gocombinatorics"

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

func Cartesian(sizes ...int) Stream

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

func Combinations(n, k int) Stream

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

func CombinationsWithReplacement(n, k int) Stream

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

func OpsPosits(k int) Stream

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

func Permutations(n, k int) Stream

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.

func Product deprecated

func Product(n, k int) Stream

Product is like Permutations except that the returned tuples may contain duplicates. For instance, Product(3, 2) yields (0,0), (0,1), (0,2), (1,0), (1,1), (1,2), (2,0), (2,1), (2,2)

Deprecated: Use TProduct 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

func TCombinations[T any](items []T, k int) *TStream[T]

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

func TCombinationsWithReplacement[T any](items []T, k int) *TStream[T]

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

func TPermutations[T any](items []T, k int) *TStream[T]

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

func TProduct[T any](items []T, k int) *TStream[T]

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

func (t *TStream[T]) Next(values []T) bool

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.

func (*TStream[T]) Reset added in v1.1.0

func (t *TStream[T]) Reset()

Reset resets this TStream to the state it had when it was first created. After calling Reset, Next will yield the first tuple.

func (*TStream[T]) TupleSize added in v1.1.0

func (t *TStream[T]) TupleSize() int

TupleSize returns the size of tuples this TStream emits. Caller must pass a slice of at least this size to the Next method.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL