Code Monkey home page Code Monkey logo

permutation's Introduction

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}]

permutation's People

Contributors

envoker avatar gitchander avatar toelsiba avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.