permutation

package module
v0.0.0-...-a5d7372 Latest Latest
Warning

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

Go to latest
Published: May 17, 2021 License: MIT Imports: 2 Imported by: 13

README

permutation

Simple permutation package for golang

Install

go get github.com/gitchander/permutation

Usage

permutations of int slice:
package main

import (
	"fmt"

	prmt "github.com/gitchander/permutation"
)

func main() {
	a := []int{1, 2, 3}
	p := prmt.New(prmt.IntSlice(a))
	for p.Next() {
		fmt.Println(a)
	}
}

result:

[1 2 3]
[2 1 3]
[3 1 2]
[1 3 2]
[2 3 1]
[3 2 1]
permutations of string slice:
package main

import (
	"fmt"

	prmt "github.com/gitchander/permutation"
)

func main() {
	a := []string{"alpha", "beta", "gamma"}
	p := prmt.New(prmt.StringSlice(a))
	for p.Next() {
		fmt.Println(a)
	}
}

result:

[alpha beta gamma]
[beta alpha gamma]
[gamma alpha beta]
[alpha gamma beta]
[beta gamma alpha]
[gamma beta alpha]
permutation use of AnySlice:
a := []interface{}{-1, "control", 9.3}

data, err := prmt.NewAnySlice(a)
if err != nil {
	log.Fatal(err)
}

p := prmt.New(data)
for p.Next() {
	fmt.Println(a)
}

or use MustAnySlice (panic if error):

a := []int{1, 2}
p := prmt.New(prmt.MustAnySlice(a))
for p.Next() {
	fmt.Println(a)
}
usage permutation.Interface
package main

import (
	"fmt"

	prmt "github.com/gitchander/permutation"
)

type Person struct {
	Name string
	Age  int
}

type PersonSlice []Person

func (ps PersonSlice) Len() int      { return len(ps) }
func (ps PersonSlice) Swap(i, j int) { ps[i], ps[j] = ps[j], ps[i] }

func main() {
	a := []Person{
		{Name: "one", Age: 1},
		{Name: "two", Age: 2},
		{Name: "three", Age: 3},
	}
	p := prmt.New(PersonSlice(a))
	for p.Next() {
		fmt.Println(a)
	}
}

result:

[{one 1} {two 2} {three 3}]
[{two 2} {one 1} {three 3}]
[{three 3} {one 1} {two 2}]
[{one 1} {three 3} {two 2}]
[{two 2} {three 3} {one 1}]
[{three 3} {two 2} {one 1}]

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type AnySlice

type AnySlice struct {
	// contains filtered or unexported fields
}

func MustAnySlice

func MustAnySlice(v interface{}) *AnySlice

func NewAnySlice

func NewAnySlice(v interface{}) (*AnySlice, error)

func (*AnySlice) Len

func (p *AnySlice) Len() int

func (*AnySlice) Swap

func (p *AnySlice) Swap(i, j int)

type IntSlice

type IntSlice []int

func (IntSlice) Len

func (p IntSlice) Len() int

func (IntSlice) Swap

func (p IntSlice) Swap(i, j int)

type Interface

type Interface interface {
	// Len is the number of elements in the collection.
	Len() int
	// Swap swaps the elements with indexes i and j.
	Swap(i, j int)
}

type Permutator

type Permutator struct {
	// contains filtered or unexported fields
}

func New

func New(v Interface) *Permutator

func (*Permutator) Next

func (p *Permutator) Next() bool

type StringSlice

type StringSlice []string

func (StringSlice) Len

func (p StringSlice) Len() int

func (StringSlice) Swap

func (p StringSlice) Swap(i, j int)

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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