itertools

package module
v0.0.0-...-40a02c1 Latest Latest
Warning

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

Go to latest
Published: May 16, 2019 License: GPL-3.0 Imports: 0 Imported by: 0

README

itertools

Itertools is a tool kit to generate combinations and permutations of go slices. It works similar to python itertools.

Installation

go get -u github.com/ernestosuarez/itertools

Usage (Examples)

After installation the package has to be imported.

import "github.com/ernestosuarez/itertools"

###Generating combinations Slices of integers

// combinations of r = 3 elements chosen from iterable
r := 3
iterable := []int{1, 2, 3, 4 }

for v := range CombinationsInt(iterable, r) {
      fmt.Println(v)

output:

[1 2 3]
[1 2 4]
[1 3 4]
[2 3 4]

Slices of strings

// combinations of r = 3 elements chosen from iterable
r := 3
iterable := []string{"A", "B", "C", "D"}

for v := range CombinationsStr(iterable, r) {
      fmt.Println(v)

output:

[A B C]
[A B D]
[A C D]
[B C D]

Custom type List

Notice you have to use the right function depending of the type of the slice. For a more general case the user could define a custom type List as follows:

type List []interface{}

Then can create a list of heterogeneous types and follow the same procedure. In this case CombinationsList has to be used.

r := 3
myList := List{1, "B", 3, 3.14}

for v := range CombinationsList(myList, r) {
      fmt.Println(v)

output:

[1 B 3]
[1 B 3.14]
[1 3 3.14]
[B 3 3.14]

###Generating permutations Slices of integers

If the number (r) of chosen element is equal to the length of the iterable, then we will obtain the r-factorial permutations

// permutations of r = 3 elements chosen from iterable with length 3
r := 3
iterable := []int{1, 2, 3}

for v := range PermutationsInt(iterable, r) {
      fmt.Println(v)

output:

[1 2 3]
[1 3 2]
[2 1 3]
[2 3 1]
[3 1 2]
[3 2 1]

On the other hand we can chose less than len(iterable) elements

// permutations of r = 3 elements chosen from a iterable of length 4
r := 3
iterable := []int{1, 2, 3, 4}

for v := range CombinationsInt(iterable, r) {
      fmt.Println(v)

output:

[1 2 3]
[1 3 2]
[2 1 3]
[2 3 1]
[3 1 2]
[3 2 1]
[1 2 4]
[1 4 2]
[2 1 4]
[2 4 1]
[4 1 2]
[4 2 1]
[1 3 4]
[1 4 3]
[3 1 4]
[3 4 1]
[4 1 3]
[4 3 1]
[2 3 4]
[2 4 3]
[3 2 4]
[3 4 2]
[4 2 3]
[4 3 2]

Slices of strings

Same idea, just use the function PermutationsStr

Custom type List

Same as in combinations, fist define the new type List (see above) and then use PermutationsList

Contributions

Contributions are welcome!

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CombinationsInt

func CombinationsInt(iterable []int, r int) chan []int

CombinationsInt generates all the combinations of r elements extracted from an slice of integers

func CombinationsList

func CombinationsList(iterable List, r int) chan List

CombinationsList generates all the combinations of r elements extracted from a List (an arbitrary list of elements). A List can be created for instance, as follows myList := List{"a", "b", 13, 3.523}

func CombinationsStr

func CombinationsStr(iterable []string, r int) chan []string

CombinationsStr generates all the combinations of r elements extracted from an slice of strings

func GenCombinations

func GenCombinations(n, r int) <-chan []int

GenCombinations generates, from two natural numbers n > r, all the possible combinations of r indexes taken from 0 to n-1. For example if n=3 and r=2, the result will be: [0,1], [0,2] and [1,2]

func GenPermutations

func GenPermutations(n int) <-chan []int

GenPermutations generates, given a number n, all the n factorial permutations of the integers from 0 to n-1

func PermutationsInt

func PermutationsInt(iterable []int, r int) chan []int

PermutationsInt generates all the permutations of r elements extracted from an slice of integers

func PermutationsList

func PermutationsList(iterable List, r int) chan List

PermutationsList generates all the permutations of r elements extracted from a List (an arbitrary list of elements). A List can be created, for instance, as follows: myList := List{"a", "b", 13, 3.523}

func PermutationsStr

func PermutationsStr(iterable []string, r int) chan []string

PermutationsStr generates all the permutations of r elements extracted from an slice of strings

Types

type List

type List []interface{}

List is a list of elements of any kind/type

Jump to

Keyboard shortcuts

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